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



描述

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

宣告

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

C++98

template <class InputIterator>
void insert (InputIterator first, InputIterator last);

C++11

template <class InputIterator>
void insert (InputIterator first, InputIterator last);

引數

  • first − 輸入迭代器,指向範圍的初始位置。

  • last − 輸入迭代器,指向範圍的結束位置。

返回值

異常

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

時間複雜度

對數,即 O(log n)

示例

以下示例演示了 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
map.htm
廣告