简体   繁体   English

STL对嵌套类进行排序

[英]STL Sort on nested Classes

I have a graph class that has a vector of nodes. 我有一个带有节点向量的图类。 In each node, there is a vertex and a STL list of edges. 在每个节点中,都有一个顶点和一个STL边列表。 Essentially it's an adjacency list. 本质上,这是一个邻接表。

For my assignment, I am trying to display the vertices with the most edges. 对于我的任务,我试图显示具有最多边的顶点。 I figured I would sort the graph's vector of nodes by edge size and print the top N vertices. 我想我可以按照边的大小对图的节点向量进行排序,并打印出前N个顶点。

So I am trying to figure out STL sort, but I'm having difficulties. 因此,我试图找出STL的排序方式,但遇到了困难。

I have 我有

 std::sort(path.begin(), path.end());

Where path is the vector of nodes (node = vertex value and list of edges) 其中path是节点的向量(node =顶点值和边列表)

In my node class, I have 在我的节点类中,我有

bool operator<(const Node<T>& rhs){
    return size < rhs.size; //size is how many edges the vertex has
}

But it's giving me errors. 但这给了我错误。 How can I construct the operator< function in my node class to work with STL sort? 如何在节点类中构造operator<函数以与STL排序一起使用?

Here are the errors: 错误如下:

$ make
g++ -c -g -std=c++0x graphBuilder.cpp
In file included from /usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../include/c++/4.5.2/algorithm:63:0,
             from graph.h:6,
             from graphBuilder.cpp:2:
/usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../include/c++/4.5.2/bits/stl_algo.h: In function '_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Node<std::basic_string<char> >*, std::vector<Node<std::basic_string<char> >, std::allocator<Node<std::basic_string<char> > > > >, _Tp = Node<std::basic_string<char> >]':
/usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../include/c++/4.5.2/bits/stl_algo.h:2249:70:   instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Node<std::basic_string<char> >*, std::vector<Node<std::basic_string<char> >, std::allocator<Node<std::basic_string<char> > > > >]'
/usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../include/c++/4.5.2/bits/stl_algo.h:2280:54:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Node<std::basic_string<char> >*, std::vector<Node<std::basic_string<char> >, std::allocator<Node<std::basic_string<char> > > > >, _Size = int]'
/usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../include/c++/4.5.2/bits/stl_algo.h:5212:4:   instantiated from 'void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Node<std::basic_string<char> >*, std::vector<Node<std::basic_string<char> >, std::allocator<Node<std::basic_string<char> > > > >]'
graph.h:32:13:   instantiated from 'void Graph<T>::topN(int) [with T = std::basic_string<char>]'
graphBuilder.cpp:10:17:   instantiated from here /usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../include/c++/4.5.2/bits/stl_algo.h:2211:4: error: passing 'const Node<std::basic_string<char> >' as 'this' argument of 'bool Node<T>::operator<(const Node<T>&) [with T = std::basic_string<char>]' discards qualifiers
make: *** [graphBuilder.o] Error 1

Your member function needs to be const -qualified: 您的成员函数必须是const限定的:

bool operator<(const Node<T>& rhs) const{

EDIT 编辑

Per request, here is a bit more how I knew that you needed to make the member function const . 对于每个请求,这里还有一点我如何知道您需要使成员函数const变得更多。

Divining the hidden meanings in stdlib-related compiler errors is something of an art, and something that you get better at with practice. 在与stdlib相关的编译器错误中划分隐藏的含义是一门艺术,并且您在实践中会变得更好。 Stdlib-related errors will often emit a whole series of compiler errors. 与Stdlib相关的错误通常会引发一系列编译器错误。 Usually the most useful of these errors is the last one, because that one is generated from the context of the code you actually wrote, rather than coming from the bowels of the library code. 通常,这些错误中最有用的是最后一个错误,因为该错误是从您实际编写的代码的上下文中生成的,而不是来自库代码的肠胃。

In this case, the last compiler error was: 在这种情况下,最后一个编译器错误是:

graphBuilder.cpp:10:17: instantiated from here /usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../include/c++/4.5.2/bits/stl_algo.h:2211:4: error: passing 'const Node >' as 'this' argument of 'bool Node::operator<(const Node&) [with T = std::basic_string]' discards qualifiers graphBuilder.cpp:10:17:从此处实例化/usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../include/c++/4.5.2/bits/ stl_algo.h:2211:4:错误: 使'const的节点 >' 为'这个'的参数'布尔节点::运算<(常量节点&)[用T =标准:: basic_string的]' 丢弃限定符

I've highlighted the illuminating bits. 我已突出显示了照明位。 This tells me that in OP's actual code, because of how the code is constructed (maybe the sort call is itself in a const member function? who knows...) the this pointer must be const , but as we can see from the posted declaration of operator< , the method is not const . 这告诉我,在OP的实际代码中,由于代码的构造方式(也许sort调用本身是在const成员函数中?谁知道...), this指针必须是const ,但是正如我们从发布中看到的那样声明operator< ,该方法不是const The "qualifiers" referred to in the compiler errors are what the Standard calls "cv-qualifiers". 编译器错误中所指的“限定词”是标准所称的“ cv限定词”。 "cv" stands for "const/volatile." “ cv”代表“ const / volatile”。

When the compiler says "Passing const X as this to Node::operator< discards qualifiers" what it's really trying to say is: 当编译器说“将const X this传递给Node::operator<丢弃限定符”时,它真正要说的是:

"You said X was const but then you tried to call a non-const member function through const X . In order for me to make this call, I would have to discard the const qualifier on X . I'm not allowed to do that, so you have to fix your code." “您说X是const,但是随后您尝试通过const X调用非const成员函数。为了进行此调用,我必须舍弃 X上的const限定符。我不允许这样做,因此您必须修正您的代码。”

The qualifiers being "discarded" here are the qualifiers on the method itself. 这里被“丢弃”的限定词是方法本身的限定词。 In other words, operator< must be a const member function, but it's not. 换句话说, operator<必须是const成员函数,但不是。

bool operator<(const Node<T>& rhs) const {
    return size() < rhs.size();
}

Making the operator const as it should be may help with not angering the compiler. 使运算符保持应有的const可能有助于避免激怒编译器。

Note: The problem arises because this is always const so the compiler expects you to promise not to change it which you specify by making the method using this to have the const qualifier. 注意:之所以会出现问题,是因为this始终是const因此编译器希望您保证不会通过使使用this的方法具有const限定符来更改指定的内容。

Alternately you could make a non-member comparison for use with sort like so: 或者,您可以进行非成员比较,以用于类似这样的排序:

<template typename T>
struct CompareNodes {
bool operator()(const Node<T>& lhs, const Node<T>& rhs) const
{
     return lhs.size() < rhs.size();
}
};

std::sort(path.cbegin(), path.cend(), CompareNodes<T>());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM