如何对向量对进行排序?
我正在尝试对 pair
向量进行排序(其中 T 是类的模板化值类型),而代码块给了我大量的错误,我不明白为什么。是否需要一些特殊语法来对向量
这是代码
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++;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
发布的代码没有编译,但是当我尝试编译它时,我注意到您可能想要:
另外:
您的这个固定版本为我编译:
这部分出现在完整代码之前已发布,但对于试图找出排序的人可能仍然有用,所以我将其保留在中:
一般来说,为了排序,您可能需要提供一种比较配对的方法,例如参见此处:
http://www.cplusplus.com/reference/algorithm/sort/
如果您使用排序算法,它将自动适用于定义了小于运算符的任何类型,但不适用于其他类型。
std::pair 应该提供默认的小于运算符,所以也许还有另一个问题 - 我们可以看到代码吗?正如托马拉克所说,这可能是因为你无法比较 T。
The posted code didn't compile but when I was trying to make it compile I noticed you probably want:
Also:
This fixed version of yours compiles for me:
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, e.g. see here:
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? As Tomalak notes, that is probably because you have no way of comparing T's.
提供您自己的比较函数(或函子)作为
sort
的最后一个参数。您的比较应该取其中的第一个并进行比较。例如:
Provide your own comparison function (or functor) as the last argument to
sort
. Your comparison should take the first of the pair and compare it.for instance:
如果您从以下开始:
为
T
添加比较器,则std::pair
的默认生成的比较器将调用:If you start with:
Add a comparator for
T
, thatstd::pair<int, T>
's default-generated comparator will invoke:看起来这些是类模板的成员函数。如果是这种情况,则
sortlevel
将需要是静态的(或非成员)才能用作std::sort()
中的比较器。另外,您在一些地方编写了例如
order[x]->first
,而它应该是order[x].first
。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 instd::sort()
.Also, you've written e.g.
order[x]->first
in a few places, when it should beorder[x].first
.每当您使用
T
时,您都需要确保它位于类或函数中,并且前面有template
。在您的代码中:
sortlevel
、get_level
和level_order
之前需要template
sort
您需要对sortlevel
进行分类。Whenever you're using
T
you need to make sure it is in a class or a function with atemplate<typename T>
before it.in your code:
template<typename T>
before the definition ofsortlevel
,get_level
andlevel_order
sort
you need to classifysortlevel<T>
.