Is std::pairlt;int, std::stringgt; ordering well-defined?(是 std::pairint, std::string订购明确?)
问题描述
似乎可以对std::vector进行排序,它会根据int值进行排序.这是一个明确定义的事情吗?
It seems that I can sort a std::vector<std::pair<int, std::string>>, and it will sort based on the int value. Is this a well defined thing to do?
std::pair 是否有基于其元素的默认排序?
Does std::pair have a default ordering based on its elements?
推荐答案
std::pair 使用字典比较:它会根据第一个元素进行比较.如果第一个元素的值相等,则根据第二个元素进行比较.
std::pair uses lexicographic comparison: It will compare based on the first element. If the values of the first elements are equal, it will then compare based on the second element.
C++03 标准(第 20.2.2 节)中的定义是:
The definition in the C++03 standard (section 20.2.2) is:
template <class T1, class T2>
bool operator<(const pair<T1, T2>& x, const pair<T1, T2>& y);
Returns: x.first < y.first || (!(y.first < x.first) && x.second < y.second).
这篇关于是 std::pair<int, std::string>订购明确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是 std::pair<int, std::string>订购明确?
基础教程推荐
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- c++ STL设置差异 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
