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