operator==,!=,<,<=,>,>=,<=>(std::map) - cppreference.com
来自cppreference.com
| 在标头 |
||
template< class Key, class T, class Compare, class Alloc > bool operator==( const std::map<Key, T, Compare, Alloc>& lhs, const std::map<Key, T, Compare, Alloc>& rhs ); |
(1) | |
template< class Key, class T, class Compare, class Alloc > bool operator!=( const std::map<Key, T, Compare, Alloc>& lhs, const std::map<Key, T, Compare, Alloc>& rhs ); |
(2) | (C++20 前) |
template< class Key, class T, class Compare, class Alloc > bool operator<( const std::map<Key, T, Compare, Alloc>& lhs, const std::map<Key, T, Compare, Alloc>& rhs ); |
(3) | (C++20 前) |
template< class Key, class T, class Compare, class Alloc > bool operator<=( const std::map<Key, T, Compare, Alloc>& lhs, const std::map<Key, T, Compare, Alloc>& rhs ); |
(4) | (C++20 前) |
template< class Key, class T, class Compare, class Alloc > bool operator>( const std::map<Key, T, Compare, Alloc>& lhs, const std::map<Key, T, Compare, Alloc>& rhs ); |
(5) | (C++20 前) |
template< class Key, class T, class Compare, class Alloc > bool operator>=( const std::map<Key, T, Compare, Alloc>& lhs, const std::map<Key, T, Compare, Alloc>& rhs ); |
(6) | (C++20 前) |
template< class Key, class T, class Compare, class Alloc > synth-three-way-result<T> operator<=>( const std::map<Key, T, Compare, Alloc>& lhs, const std::map<Key, T, Compare, Alloc>& rhs ); |
(7) | (C++20 起) |
比较两个 map 的内容。
1,2) 检查 lhs 与 rhs 的内容是否相等,即它们是否拥有相同数量的元素且 lhs 中每个元素与 rhs 的同位置元素比较相等。
3-6) 按字典序比较 lhs 与 rhs 的内容。由等价于 std::lexicographical_compare 的函数进行比较。此比较忽略 map 的定序 Compare。
7) 按字典序比较 lhs 与 rhs 的内容。如同通过调用 std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), synth-three-way) 进行比较。此比较忽略 map 的定序 Compare。
|
|
(C++20 起) |
参数
返回值
1) 在 map 内容相等时返回 true,否则返回 false
2) 在 map 内容不相等时返回 true,否则返回 false
3) 在 lhs 的内容按字典序小于 rhs 的内容时返回 true,否则返回 false
4) 在 lhs 的内容按字典序小于 或等于 rhs 的内容时返回 true,否则返回 false
5) 在 lhs 的内容按字典序大于 rhs 的内容时返回 true,否则返回 false
6) 在 lhs 的内容按字典序大于 或等于 rhs 的内容时返回 true,否则返回 false
7) lhs 与 rhs 中的首对不等价元素的相对顺序,如果有这种元素;否则是 lhs.size() <=> rhs.size()。
复杂度
1,2) lhs 与 rhs 的大小不同时是常数,否则与 map 的大小成线性
3-7) 与 map 的大小成线性
注解
|
各关系运算符是基于各个元素的 |
(C++20 前) |
|
各关系运算符是基于 synth-three-way 定义的,若有可能则它会使用 要注意,如果元素类型自身并不提供 |
(C++20 起) |
示例
#include <cassert> #include <compare> #include <map> int main() { std::map<int, char> a{{1, 'a'}, {2, 'b'}, {3, 'c'}}; std::map<int, char> b{{1, 'a'}, {2, 'b'}, {3, 'c'}}; std::map<int, char> c{{7, 'Z'}, {8, 'Y'}, {9, 'X'}, {10, 'W'}}; assert ("" "比较相等的容器:" && (a != b) == false && (a == b) == true && (a < b) == false && (a <= b) == true && (a > b) == false && (a >= b) == true && (a <=> b) != std::weak_ordering::less && (a <=> b) != std::weak_ordering::greater && (a <=> b) == std::weak_ordering::equivalent && (a <=> b) >= 0 && (a <=> b) <= 0 && (a <=> b) == 0 && "比较不相等的容器:" && (a != c) == true && (a == c) == false && (a < c) == true && (a <= c) == true && (a > c) == false && (a >= c) == false && (a <=> c) == std::weak_ordering::less && (a <=> c) != std::weak_ordering::equivalent && (a <=> c) != std::weak_ordering::greater && (a <=> c) < 0 && (a <=> c) != 0 && (a <=> c) <= 0 && ""); }
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 3431 | C++20 | operator<=> 不需要 T 实现 three_way_comparable
|
需要 |