本文介紹了使用 STL 排序功能對列表進行排序的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試按降序對包含 struct
項的列表(類的一部分)進行排序,但它無法編譯:
I'm trying to sort a list (part of a class) in descending order containing items of a struct
, but it doesn't compile:
錯誤:__last - __first"中的operator-"不匹配
error: no match for 'operator-' in '__last - __first'
sort(Result.poly.begin(), Result.poly.end(), SortDescending());
這里是SortDescending
:
struct SortDescending
{
bool operator()(const term& t1, const term& t2)
{
return t2.pow < t1.pow;
}
};
誰能告訴我出了什么問題?
Can anyone tell me what's wrong?
推薦答案
標準算法 std::sort
需要隨機訪問迭代器,其中 std::list<>::iterator
s 不是(列表迭代器是雙向迭代器).
The standard algorithm std::sort
requires random access iterators, which std::list<>::iterator
s are not (list iterators are bidirectional iterators).
您應該使用 std::list<>::sort
成員函數.
You should use the std::list<>::sort
member function.
這篇關于使用 STL 排序功能對列表進行排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!