C++ 對映庫 - insert() 函式



描述

C++ 函式std::map::insert() 透過從初始化列表插入新元素來擴充套件對映。此函式將容器大小增加一。

宣告

以下是來自std::map 標頭檔案的 std::map::insert() 函式的宣告。

C++11

void insert (initializer_list<value_type> il);

引數

il − 初始化列表。

返回值

無。

異常

此成員函式不會丟擲異常。

時間複雜度

對數,即 O(log n)。

示例

以下示例演示了 std::map::insert() 函式的使用。

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   map<char, int> m = {
            {'b', 2},
            {'c', 3},
            {'d', 4},
            };

   m.insert({{'e', 5}, {'a', 1}});

   cout << "Map contains following elements" << endl;

   for (auto it = m.begin(); it != m.end(); ++it)
      cout << it->first << " = " << it->second << endl;

   return 0;
}

讓我們編譯並執行上述程式,這將產生以下結果:

Map contains following elements
a = 1
b = 2
c = 3
d = 4
e = 5
map.htm
廣告
© . All rights reserved.