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



描述

C++ 函式std::multimap::insert() 使用移動語義擴充套件容器,透過插入新元素到 multimap 中。此函式使容器大小增加一。

宣告

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

C++11

template <class P>
iterator insert (const_iterator position, P&& val);

引數

  • position − 插入元素的位置提示。

  • 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(m.begin(), move(pair<char, int>('a', 0)));

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

   return 0;
}

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

After inserting new element iterator points to
a = 0
map.htm
廣告