[英]Sort vector of class in c++
我在排序功能方面遇到了一些麻煩...這是我的代碼:
class Parola {
public:
string s;
int repetition;
bool operator()(const Parola *x, const Parola *y) {
return x->repetition > y->repetition;
}
};
int main(int argc, char** argv) {
...
vector<Parola> p;
...
some insertions here
...
sort(p.begin(), p.end(), Parola());
...
return 0;
}
為什么我不能沒有錯誤地編譯它? 非常感謝!
PS:我只會告訴你超過五十個錯誤的前三行:
/usr/include/c++/4.2.1/bits/stl_algo.h: In function 'const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare) [with _Tp = Parola, _Compare = Parola]':
/usr/include/c++/4.2.1/bits/stl_algo.h:2795: instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Parola*, std::vector<Parola, std::allocator<Parola> > >, _Size = long int, _Compare = Parola]'
/usr/include/c++/4.2.1/bits/stl_algo.h:2866: instantiated from 'void std::sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Parola*, std::vector<Parola, std::allocator<Parola> > >, _Compare = Parola]'
您的比較器需要指針,但該向量包含Parola
實例。 你需要改變它。 但最簡單的方法是實現一個小於比較運算符。
class Parola {
public:
string s;
int repetition;
};
bool operator<(const Parola& x, const Parola& y) {
return x.repetition < y.repetition;
}
然后你可以調用sort
而不需要第三個參數:
sort(p.begin(), p.end());
為OP提供一些可供選擇的選項:(注意:並非詳盡無遺)
class Parola {
public:
string s;
int repetition;
bool operator<(const Parola& x) const
{
return repetition < x.repetition;
}
}
使用默認的std :: less <>模板調用。
sort(p.begin(), p.end());
class Parola {
public:
string s;
int repetition;
bool operator()(const Parola& x, const Parola& y) const
{
return x.repetition < y.repetition;
}
}
使用可選的比較對象調用,如dasblinken所指出的,奇怪,但有效:
std::sort(p.begin(), p.end(), Parola());
bool operator <(const Parola& x, const Parola& y)
{
x.repetition < y.repetition;
}
這與(1)一樣,使用默認的std :: less <>比較器,但要求外部運算符也是Parola類的朋友,如果聲明這樣,則可以訪問私有數據成員。 其用途與(1)相同。
class CompareParola
{
public:
bool operator ()(const Parola& x, const Parola& y) const
{
return x.repetition < right.repetition;
}
};
並用於:
std::sort(p.begin(), p.end(), CompareParola());
如(3),如果被訪問的成員是私有的,則必須將CompareParola類別與Parola建立聯系:
bool ParolaLess(const Parola& x, const Parola& y)
{
return x.repetition < y.repetition;
}
類似於外部運算符或外部函數類,這還需要與對象類相關聯以獲得對私有成員的訪問。 像這樣調用:
std::sort(p.begin(), p.end(), ParolaLess);
class Parola {
public:
string s;
int repetition;
static bool Less(const Parola& x, const Parola& y)
{
return x.repetition < y.repetition;
}
};
這通常未被充分利用,並且具有訪問所有對象成員變量的非常好的屬性,包括私有變量(顯然,它是用類定義的)。 你可以這樣做:
std::sort(p.begin(), p.end(), Parola::Less)
請注意,這與(1)和(2)一樣,保留了類中的所有內容。
在所有這些中,我更喜歡(1)因為它的簡單性,(4)因為它的獨立性,但每個人都有自己的品味。 有時(5)或(6)真的派上用場(我是(6)的個人粉絲)。
如果你能想到更多並讓代表編輯它,請根據需要更新它。 請盡量使它們至少有些有用= P.
你的直接問題是你的比較運算符沒有通過Parola const*
對象而是Parola const&
objects:迭代器被取消引用以獲得實際比較的值。
下一個問題是您可能不應該嘗試將比較對象捆綁到實際對象中:比較對象實際上不像Parola
對象。 你想要一個單獨的比較器,然后與std::sort()
:
sturct ParolaCompare {
bool operator()(Parola const& p0, Parola const& p1) const {
// return something defining a strict weak order on Parola objects
}
};
// ...
std::sort(v.begin(), v.end(), ParolaCompare());
或者,您可以為Parola
對象定義合適的operator<()
:
bool operator< (Parola const& p0, Parola const& p1) {
// again, a strict weak order on Parola objects
}
使運算符采用const引用而不是指針。 即
bool operator()(const Parola &x, const Parola &y) {
return x.repetition > y.repetition;
}
通常,當你有一個你希望STL為你排序的對象時,無論是使用sort方法,還是將它放在std :: set中,你需要一個operator <方法。
因此,最好讓它成為班上的一員。 該方法的簽名應該類似於:bool operator <(const Parola&left,const Parola&right)
你如何實現它取決於你的類本身。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.