multimap::insert
描述 (Description)
C ++函数std::multimap::insert()通过在multimap中插入新元素来扩展容器。
声明 (Declaration)
以下是std :: multimap :: insert()函数形式std :: map头的声明。
C++98
template <class InputIterator>
void insert (InputIterator first, InputIterator last);
C++11
template <class InputIterator>
void insert (InputIterator first, InputIterator last);
参数 (Parameters)
first - 将迭代器输入到范围内的初始位置。
last - 将迭代器输入到范围内的最终位置。
返回值
没有
异常 (Exceptions)
如果抛出异常,对容器没有影响。
时间复杂
对数即O(log n)
例子 (Example)
以下示例显示了std :: multimap :: insert()函数的用法。
#include <iostream>
#include <map>
using namespace std;
int main(void) {
/* Multimap with duplicates */
multimap<char, int> m1 {
{'a', 1},
{'a', 2},
{'b', 3},
{'c', 4},
{'d', 5}
};
multimap<char, int> m2;
m2.insert(m1.begin(), m1.end());
cout << "Multimap contains the following elements:" << endl;
for (auto it = m2.begin(); it != m2.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
Multimap contains the following elements:
a = 1
a = 2
b = 3
c = 4
d = 5