operator==,!=,<,<=,>,>=,<=>(std::multimap)

来自cppreference.com
< cpp‎ | container‎ | multimap

 
 
 
 
在标头 <map> 定义
template< class Key, class T, class Compare, class Alloc >

bool operator==( const std::multimap<Key, T, Compare, Alloc>& lhs,

                 const std::multimap<Key, T, Compare, Alloc>& rhs );
(1)
template< class Key, class T, class Compare, class Alloc >

bool operator!=( const std::multimap<Key, T, Compare, Alloc>& lhs,

                 const std::multimap<Key, T, Compare, Alloc>& rhs );
(2) (C++20 前)
template< class Key, class T, class Compare, class Alloc >

bool operator<( const std::multimap<Key, T, Compare, Alloc>& lhs,

                const std::multimap<Key, T, Compare, Alloc>& rhs );
(3) (C++20 前)
template< class Key, class T, class Compare, class Alloc >

bool operator<=( const std::multimap<Key, T, Compare, Alloc>& lhs,

                 const std::multimap<Key, T, Compare, Alloc>& rhs );
(4) (C++20 前)
template< class Key, class T, class Compare, class Alloc >

bool operator>( const std::multimap<Key, T, Compare, Alloc>& lhs,

                const std::multimap<Key, T, Compare, Alloc>& rhs );
(5) (C++20 前)
template< class Key, class T, class Compare, class Alloc >

bool operator>=( const std::multimap<Key, T, Compare, Alloc>& lhs,

                 const std::multimap<Key, T, Compare, Alloc>& rhs );
(6) (C++20 前)
template< class Key, class T, class Compare, class Alloc >

/* see below */ operator<=>( const std::multimap<Key, T, Compare, Alloc>& lhs,

                             const std::multimap<Key, T, Compare, Alloc>& rhs );
(7) (C++20 起)

比较两个 multimap 的内容。

1,2) 检查 lhs 与 rhs 的内容是否相等,即它们是否拥有相同数量的元素且 lhs 中每个元素与 rhs 的同位置元素比较相等。
3-6) 按字典序比较 lhs 与 rhs 的内容。由等价于 std::lexicographical_compare 的函数进行比较。此比较忽略 multimap 的定序 Compare。
7) 按字典序比较 lhs 与 rhs 的内容。如同通过在两个 multimap 上以进行合成三路比较(见后述)的函数对象调用 std::lexicographical_compare_three_way 进行比较。返回类型同合成三路比较的结果类型。此比较忽略 multimap 的定序 Compare。

给定分别作为左操作数与右操作数的两个 const E 左值 lhs 与 rhs(其中 E 是 std::pair<const Key, T>),合成三路比较定义如下:

  • 在 std::three_way_comparable_with<E, E> 得到满足时等价于 lhs <=> rhs;
  • 否则,在以 operator< 比较两个 const E 左值良构且结果类型满足 boolean-testable 时等价于
lhs < rhs ? std::weak_ordering::less :
rhs < lhs ? std::weak_ordering::greater :
            std::weak_ordering::equivalent
  • 否则,不定义合成三路比较,而且 operator<=> 不会参与重载决议。
如果 three_way_comparable_with 或 boolean-testable 被满足但未被实现,或使用 operator< 但 E 与 < 不建立全序,那么 operator<=> 的行为未定义。

<、 <=、 >、 >= 及 != 运算符分别从 operator<=> 与 operator== 合成。

(C++20 起)

参数

lhs, rhs - 要比较内容的 multimap
-
为使用重载 (1,2), T, Key 必须符合可相等比较 (EqualityComparable) 的要求。
-
为使用重载 (3-6), Key 必须符合可小于比较 (LessThanComparable) 的要求。顺序关系必须建立全序。

返回值

1) 在 multimap 内容相等时返回 true,否则返回 false
2) 在 multimap 内容不相等时返回 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 的大小不同时是常数,否则与 multimap 的大小呈线性
3-7) 与 multimap 的大小呈线性

示例

#include <iostream>
#include <map>
 
int main()
{
    std::multimap<int, char> alice{{1, 'a'}, {2, 'b'}, {3, 'c'}};
    std::multimap<int, char> bob{{7, 'Z'}, {8, 'Y'}, {9, 'X'}, {10, 'W'}};
    std::multimap<int, char> eve{{1, 'a'}, {2, 'b'}, {3, 'c'}};
 
    std::cout << std::boolalpha;
 
    // 比较不相等的容器
    std::cout << "alice == bob returns " << (alice == bob) << '\n';
    std::cout << "alice != bob returns " << (alice != bob) << '\n';
    std::cout << "alice <  bob returns " << (alice < bob) << '\n';
    std::cout << "alice <= bob returns " << (alice <= bob) << '\n';
    std::cout << "alice >  bob returns " << (alice > bob) << '\n';
    std::cout << "alice >= bob returns " << (alice >= bob) << '\n';
 
    std::cout << '\n';
 
    // 比较相等的容器
    std::cout << "alice == eve returns " << (alice == eve) << '\n';
    std::cout << "alice != eve returns " << (alice != eve) << '\n';
    std::cout << "alice <  eve returns " << (alice < eve) << '\n';
    std::cout << "alice <= eve returns " << (alice <= eve) << '\n';
    std::cout << "alice >  eve returns " << (alice > eve) << '\n';
    std::cout << "alice >= eve returns " << (alice >= eve) << '\n';
}

输出:

alice == bob returns false
alice != bob returns true
alice <  bob returns true
alice <= bob returns true
alice >  bob returns false
alice >= bob returns false
 
alice == eve returns true
alice != eve returns false
alice <  eve returns false
alice <= eve returns true
alice >  eve returns false
alice >= eve returns true