C++ Multimap 库 - multimap() 函数
描述
C++ 函数 std::multimap::multimap() 从初始化列表构造一个多重映射。
声明
以下是 std::multimap::multimap() 函数形式 std::map 头的声明。
C++11
multimap (initializer_list<value_type> il,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
参数
il − 初始化列表。
comp − 一个二元谓词,它接受两个关键参数,如果第一个参数在第二个之前,则返回 true,否则返回 false。 默认情况下,它使用较少的谓词。
alloc − 分配器对象。
返回值
构造函数从不返回值。
异常
如果抛出异常,对容器没有影响。
时间复杂度
线性,即 O(n)
示例
以下示例显示了 std::multimap::multimap() 函数的用法。
#include <iostream>
#include <map>
using namespace std;
int main(void) {
/* Multimap with duplicates */
multimap<char, int> m {
{'a', 1},
{'a', 2},
{'b', 3},
{'c', 4},
{'c', 5},
};
cout << "Multimap contains following elements:" << endl;
for (auto it = m.begin(); it != m.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
Multimap contains following elements: a = 1 a = 2 b = 3 c = 4 c = 5

