[英]how to sort a vector of pairs?
I am trying to sort a vector of pair<int, T>
(where T is the templated value type of the class) and codeblocks is giving me massive amounts of errors i dont understand why. 我正在尝试
pair<int, T>
(其中T是类的模板化值类型)的向量进行排序pair<int, T>
而代码块给了我大量的错误,我不明白为什么。 is there there some special syntax required to sort vector<pair<int, T> >
? 是否有一些特殊的语法需要对
vector<pair<int, T> >
进行排序? i did make the comparison function, which did not use T at all 我做了比较功能,根本没有使用T
here's the code 这是代码
bool sortlevel(pair<int, T> a, pair<int, T> b){
return a.first < b.first;
}
void get_level(Node<T> * root, vector <pair<int, T> > & order, int level = 0){
// changes the tree to a vector
if (root){
order.push_back(pair(level, root -> value));
get_level(root -> left, order, level + 1);
get_level(root -> right, order, level + 1);
}
}
void level_order(ostream & ostr ) {
vector<pair<int, T> > order;
get_level(root_, order);
sort(order.begin(), order.end(), sortlevel);
int max_level = order[order.size() - 1] -> first;
int x = 0;
int current_level = order[x] -> first;
while (x < order.size()){
for(int y = 0; y < current_level - x; y++)
cout << "\t";
while (order[x] -> first == current_level){
cout << order[x] -> second << " ";
x++;
}
}
}
The posted code didn't compile but when I was trying to make it compile I noticed you probably want: 发布的代码未编译,但是当我尝试对其进行编译时,我注意到您可能想要:
order.push_back(std::make_pair(level, root -> value));
Also: 也:
int max_level = order[order.size() - 1]. first;
This fixed version of yours compiles for me: 您的这个固定版本为我编译 :
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class T
{};
template <class T> class Node
{
public:
T value;
Node<T>* left;
Node<T>* right;
};
Node<T>* root_;
bool sortlevel(pair<int, T> a, pair<int, T> b){
return a.first < b.first;
}
void get_level(Node<T> * root, vector <pair<int, T> > & order, int level = 0){
// changes the tree to a vector
if (root){
order.push_back(std::make_pair(level, root -> value));
get_level(root -> left, order, level + 1);
get_level(root -> right, order, level + 1);
}
}
void level_order(ostream & ostr ) {
vector<pair<int, T> > order;
get_level(root_, order);
sort(order.begin(), order.end(), sortlevel);
int max_level = order[order.size() - 1]. first;
int x = 0;
int current_level = order[x].first;
while (x < order.size()){
for(int y = 0; y < current_level - x; y++)
cout << "\t";
while (order[x]. first == current_level){
// cout << order[x]. second << " ";
x++;
}
}
}
This part came before the full code was posted but may still be useful to someone trying to figure out sort so I'll keep it in : Generally for sorting you may need to provide a way of comparing the pairs, eg see here: http://www.cplusplus.com/reference/algorithm/sort/ 这部分是在完整代码发布之前进行的,但是对于试图弄清排序的人可能仍然有用,因此我将其保留在其中 :通常,对于排序,您可能需要提供一种比较对的方法,例如,请参见: http: //www.cplusplus.com/reference/algorithm/sort/
If you use the sort algorithm it will work automagically for anything that has the less than operator defined but not for other types. 如果使用排序算法,它将自动适用于定义了小于运算符但不适用于其他类型的任何内容。
std::pair should provide a default less than operator so perhaps there is another issue - can we see the code? std :: pair应该提供小于操作符的默认值,因此也许还有另一个问题-我们可以看到代码吗? As Tomalak notes, that is probably because you have no way of comparing T's.
正如Tomalak指出的那样,这可能是因为您无法比较T。
Provide your own comparison function (or functor) as the last argument to sort
. 提供您自己的比较函数(或函子)作为
sort
的最后一个参数。 Your comparison should take the first of the pair and compare it. 您的比较应该采用一对中的第一个进行比较。
for instance: 例如:
template<typename T>
bool myfunction (const pair<int, T>& i, const pair<int, T>& j)
{
return (i.first < j.first);
}
sort (myvector.begin(), myvector.end(), myfunction<ActualType>);
If you start with: 如果您从以下内容开始:
#include <vector>
#include <algorithm>
struct T {
T(int x) : x(x) {};
int x;
};
int main() {
std::vector<std::pair<int, T> > v;
std::pair<int, T> a = std::make_pair(0, T(42));
std::pair<int, T> b = std::make_pair(1, T(36));
v.push_back(a);
v.push_back(b);
std::sort(v.begin(), v.end());
}
Add a comparator for T
, that std::pair<int, T>
's default-generated comparator will invoke: 为
T
添加一个比较器, std::pair<int, T>
的默认生成的比较器将调用:
struct T {
T(int x) : x(x) {};
bool operator<(const T& other) const {
return x < other.x;
}
int x;
};
It looks like these are member functions of a class template. 看起来这些是类模板的成员函数。 If that is the case, then
sortlevel
will need to be static (or a non-member) in order to be used as the comparator in std::sort()
. 如果是这种情况,则
sortlevel
必须是静态的(或非成员的)才能用作std::sort()
的比较器。
Also, you've written eg order[x]->first
in a few places, when it should be order[x].first
. 同样,您在一些地方编写了例如
order[x]->first
,而应该是order[x].first
。
Whenever you're using T
you need to make sure it is in a class or a function with a template<typename T>
before it. 每当使用
T
都需要确保它在类或带有template<typename T>
的函数中。
in your code: 在您的代码中:
template<typename T>
before the definition of sortlevel
, get_level
and level_order
sortlevel
, get_level
和level_order
的定义之前,需要template<typename T>
sort
you need to classify sortlevel<T>
. sort
您需要对sortlevel<T>
进行分类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.