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>>通过
基础教程推荐
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- c++ STL设置差异 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
