C++ Map 库 - operator< 函数
描述
C++ 函数 std::map::operator< 测试第一个映射是否小于其他映射。
运算符 < 按顺序比较元素,比较在第一次不匹配时停止。
声明
以下是 std::map::operator< 函数形式 std::map 标头的声明。
C++98
template <class Key, class T, class Compare, class Alloc>
bool operator< ( const map<Key,T,Compare,Alloc>& m1,
const map<Key,T,Compare,Alloc>& m2);
参数
m1 − 第一个映射对象。
m2 − 第二个映射对象。
返回值
如果第一个映射小于第二个,则返回 true,否则返回 false。
异常
这个函数不会抛出异常。
时间复杂度
线性,即 O(n)
示例
以下示例显示了 std::map::operator< 函数的用法。
#include <iostream>
#include <map>
using namespace std;
int main(void) {
map<char, int> m1;
map<char, int> m2;
m2.emplace('a', 1);
if (m1 < m2)
cout << "Map m1 is less than m2." << endl;
m1 = m2;
if (!(m1 < m2))
cout << "Map m1 is not less than m2." << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
Map m1 is less than m2. Map m1 is not less than m2.

