本文介紹了排序 std::vector<std::pair<std::string,bool>>通過字符串?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如何通過比較作為 std::string
的 pair.first
來對這個 vector
進行排序?(不提供靜態比較函數,也不使用 boost).
How can I sort this vector
by comparing the pair.first
which is an std::string
? (without providing a static compare function, nor use boost).
推薦答案
std::vector<std::pair<std::string, bool> > v;
std::sort(v.begin(), v.end());
std::pair
重載 operator<
先按 first
元素排序,然后按 second
元素排序.因此,如果您僅使用默認排序順序 (operator<
) 對 vector
進行排序,您將獲得所需的排序.
std::pair
overloads operator<
to sort first by the first
element then by the second
element. Thus, if you just sort the vector
using the default sort ordering (operator<
), you'll get your desired ordering.
這篇關于排序 std::vector<std::pair<std::string,bool>>通過字符串?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!