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



描述

C++ 函式std::multimap::insert()透過在多重對映中插入新元素來擴充套件容器。此函式將容器大小增加一。

宣告

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

C++98

iterator insert (const value_type& val);

C++11

iterator insert (const value_type& val);

引數

val - 要插入的值。

返回值

返回一個指向新插入元素的迭代器。

異常

如果丟擲異常,則對容器沒有影響。

時間複雜度

對數級,即 O(log n)

示例

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

#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},
         };

   auto pos = m.insert(pair<char, int>('d', 5));

   cout << "After inserting new element iterator points to" << endl;
   cout << pos->first << " = " << pos->second << endl;

   return 0;
}

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

After inserting new element iterator points to
d = 5
map.htm
廣告