本文介紹了排序 std::vector<std::pair<std::string,bool>>通過字符串?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
如何通過比較作為 std::string
的 pair.first
來對這個 vector
進(jìn)行排序?(不提供靜態(tài)比較函數(shù),也不使用 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
元素排序.因此,如果您僅使用默認(rèn)排序順序 (operator<
) 對 vector
進(jìn)行排序,您將獲得所需的排序.
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.
這篇關(guān)于排序 std::vector<std::pair<std::string,bool>>通過字符串?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!