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>>通过


- GDB 不显示函数名 2022-01-01
- OpenGL 对象的 RAII 包装器 2021-01-01
- 如何提取 __VA_ARGS__? 2022-01-01
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
- XML Schema 到 C++ 类 2022-01-01
- 将函数的返回值分配给引用 C++? 2022-01-01
- DoEvents 等效于 C++? 2021-01-01
- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- 哪个更快:if (bool) 或 if(int)? 2022-01-01
- 将 hdc 内容复制到位图 2022-09-04