C++ 對映庫 - operator= 函式



描述

C++ 函式std::multimap::operator= 將初始化列表中的元素複製到多重對映。

宣告

以下是來自 std::map 標頭檔案的 std::multimap::operator= 函式的宣告。

C++11

multimap& operator= (initializer_list<value_type> il);

引數

il - 初始化列表。

返回值

返回 this 指標

異常

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

時間複雜度

線性,即 O(n)

示例

以下示例演示了 std::multimap::operator= 函式的使用方法。

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   multimap<char, int> m;

   m = {
         {'a', 1},
         {'a', 2},
         {'b', 3},
         {'c', 4},
         {'d', 5}
       };

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

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

   return 0;
}

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

Multimap contains following elements
a = 1
a = 2
b = 3
c = 4
d = 5
map.htm
廣告