C++ multimap :: operator< 函数



C++ 的std::multimap::operator<()函数用于根据键值对按字典顺序比较两个 multimap。比较使用键值对,首先比较键,如果键相等,则比较值。如果第一个 multimap 中的第一个元素小于第二个 multimap 中的对应元素,则返回 true,否则返回 false。

语法

以下是 std::multimap::operator<() 函数的语法。

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

参数

  • lhs − 指示第一个 map 对象。
  • rhs − 指示第二个 map 对象。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

返回值

如果第一个 map 小于第二个 map,则此函数返回 true,否则返回 false。

异常

此函数不抛出异常。

时间复杂度

此函数的时间复杂度为线性,即 O(n)

示例

让我们来看下面的例子,我们将演示 operator<() 函数的使用。

Open Compiler
#include <iostream> #include <map> using namespace std; int main(void) { multimap<char, int> m1; multimap<char, int> m2; m2.insert(pair<char, int>('a', 1)); if (m1 < m2) cout << "m1 multimap is less than m2." << endl; m1 = m2; if (!(m1 < m2)) cout << "m1 multimap is not less than m2." << endl; return 0; }

输出

以下是上述代码的输出:

m1 multimap is less than m2.
m1 multimap is not less than m2.
multimap.htm
广告