排序std :: vector< std :: pair< std :: string,bool>>由字符串? [英] Sorting a std::vector<std::pair<std::string,bool>> by the string?

查看:237
本文介绍了排序std :: vector< std :: pair< std :: string,bool>>由字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过比较 pair.first 这是来排序向量 std :: string ? (不提供静态比较函数,也不使用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 overloads 运算符< 首先通过首先元素,然后通过第二 。因此,如果你只是使用默认排序顺序( operator )对向量进行排序,订购。

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&lt; std :: pair&lt; std :: string,bool&gt;&gt;由字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆