C++ 無序對映庫 - operator= 函式



描述

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

宣告

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

C++11

unordered_map& operator=(intitializer_list<value_type> il);

引數

il - 初始化列表。

返回值

返回 this 指標

時間複雜度

線性,即平均情況下為 O(n)。

二次,即最壞情況下為 O(n2)。

示例

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

#include <iostream>
#include <unordered_map>

using namespace std;

int main(void) {
   unordered_map<char, int> um;

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

   cout << "Unordered map contains following elements: " << endl;

   for (auto it = um.cbegin(); it != um.cend(); ++it)
      cout << it->first << " = " << it->second << endl;

   return 0;
}

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

Unordered map contains following elements: 
e = 5
d = 4
c = 3
b = 2
a = 1
unordered_map.htm
廣告