命名空间
变体
操作

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

来自 cppreference.cn
< 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) (until 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) (until 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) (until 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) (until 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) (until C++20)
template< class Key, class T, class Compare, class Alloc >

synth-three-way-result<T>
    operator<=>( const std::multimap<Key, T, Compare, Alloc>& lhs,

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

比较两个 multimap 的内容。

1,2) 检查 lhsrhs 的内容是否相等,即它们是否具有相同数量的元素,并且 lhs 中的每个元素与 rhs 中相同位置的元素比较相等。
3-6) 字典序比较 lhsrhs 的内容。 比较通过等价于 std::lexicographical_compare 的函数执行。 此比较忽略 multimap 的排序 Compare
7) 字典序比较 lhsrhs 的内容。 比较的执行如同调用 std::lexicographical_compare_three_way(lhs.begin(), lhs.end(),
                                       rhs.begin(), rhs.end(), synth-three-way)
。此比较忽略 multimap 的排序 Compare
返回类型是 synth-three-way 的返回类型(即 synth-three-way-result <T>)。
若不满足下列条件之一,则行为未定义
  • T model three_way_comparable
  • 为类型 T (可能带 const 限定)的值定义了 <,且 < 是全序关系。

<<=>>=!= 运算符分别从 operator<=>operator== 合成

(since C++20)

内容

[edit] 参数

lhs, rhs - 要比较其内容的 multimap
-
T, Key 必须满足 EqualityComparable 的要求,以便使用重载 (1,2)。
-
Key 必须满足 LessThanComparable 的要求,以便使用重载 (3-6)。 排序关系必须建立全序。

[edit] 返回值

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) 若存在非等价元素对,则为 lhsrhs 中首对非等价元素的相对顺序,否则为 lhs.size() <=> rhs.size()

[edit] 复杂度

1,2)lhsrhs 大小不同则为常数,否则与 multimap 的大小成线性关系。
3-7)multimap 的大小成线性关系。

[edit] 注解

关系运算符根据元素类型的 operator< 定义。

(until C++20)

关系运算符根据 synth-three-way 定义,它尽可能使用 operator<=>,否则使用 operator<

值得注意地,若元素自身不提供 operator<=>,但可隐式转换到三路比较类型,则会使用该转换,而非 operator<

(since C++20)

[edit] 示例

#include <cassert>
#include <compare>
#include <map>
 
int main()
{
    std::multimap<int, char> a{{1, 'a'}, {2, 'b'}, {3, 'c'}};
    std::multimap<int, char> b{{1, 'a'}, {2, 'b'}, {3, 'c'}};
    std::multimap<int, char> c{{7, 'Z'}, {8, 'Y'}, {9, 'X'}, {10, 'W'}};
 
    assert
    (""
        "Compare equal containers:" &&
        (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 &&
 
        "Compare non equal containers:" &&
        (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 &&
    "");
}

[edit] 缺陷报告

下列行为变更缺陷报告被追溯性地应用于先前发布的 C++ 标准。

DR 应用于 发布时的行为 正确行为
LWG 3431 C++20 operator<=> 不要求 T
建模 three_way_comparable
要求