簡體   English   中英

STL對嵌套類進行排序

[英]STL Sort on nested Classes

我有一個帶有節點向量的圖類。 在每個節點中,都有一個頂點和一個STL邊列表。 本質上,這是一個鄰接表。

對於我的任務,我試圖顯示具有最多邊的頂點。 我想我可以按照邊的大小對圖的節點向量進行排序,並打印出前N個頂點。

因此,我試圖找出STL的排序方式,但遇到了困難。

我有

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

其中path是節點的向量(node =頂點值和邊列表)

在我的節點類中,我有

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

但這給了我錯誤。 如何在節點類中構造operator<函數以與STL排序一起使用?

錯誤如下:

$ 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

您的成員函數必須是const限定的:

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

編輯

對於每個請求,這里還有一點我如何知道您需要使成員函數const變得更多。

在與stdlib相關的編譯器錯誤中划分隱藏的含義是一門藝術,並且您在實踐中會變得更好。 與Stdlib相關的錯誤通常會引發一系列編譯器錯誤。 通常,這些錯誤中最有用的是最后一個錯誤,因為該錯誤是從您實際編寫的代碼的上下文中生成的,而不是來自庫代碼的腸胃。

在這種情況下,最后一個編譯器錯誤是:

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的]' 丟棄限定符

我已突出顯示了照明位。 這告訴我,在OP的實際代碼中,由於代碼的構造方式(也許sort調用本身是在const成員函數中?誰知道...), this指針必須是const ,但是正如我們從發布中看到的那樣聲明operator< ,該方法不是const 編譯器錯誤中所指的“限定詞”是標准所稱的“ cv限定詞”。 “ cv”代表“ const / volatile”。

當編譯器說“將const X this傳遞給Node::operator<丟棄限定符”時,它真正要說的是:

“您說X是const,但是隨后您嘗試通過const X調用非const成員函數。為了進行此調用,我必須舍棄 X上的const限定符。我不允許這樣做,因此您必須修正您的代碼。”

這里被“丟棄”的限定詞是方法本身的限定詞。 換句話說, operator<必須是const成員函數,但不是。

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

使運算符保持應有的const可能有助於避免激怒編譯器。

注意:之所以會出現問題,是因為this始終是const因此編譯器希望您保證不會通過使使用this的方法具有const限定符來更改指定的內容。

或者,您可以進行非成員比較,以用於類似這樣的排序:

<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