Sorting a std::vectorlt;std::pairlt;std::string,boolgt;gt; by the string?(排序 std::vectorstd::pairstd::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>>通过字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:排序 std::vector<std::pair<std::string,bool>>通过
基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
