目录

map::insert

描述 (Description)

C ++函数std::map::insert()通过在地图中插入新元素来扩展容器。

声明 (Declaration)

以下是std :: map :: 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 :: map :: insert()函数的用法。

#include <iostream>
#include <map>
using namespace std;
int main(void) {
   /* Initializer_list constructor */
   map<char, int> m1 = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };
   map<char, int> m2;
   m2.insert(m1.begin(), m1.end());
   cout << "Map contains following elements" << endl;
   for (auto it = m2.begin(); it != m2.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

Map contains following elements
a = 1
b = 2
c = 3
d = 4
e = 5
↑回到顶部↑
WIKI教程 @2018