當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Unordered_map unordered_map()用法及代碼示例


描述

C++ 函數std::unordered_map::unordered_map()使用移動語義用 other 的內容構造一個 unordered_map。

聲明

以下是 std::unordered_map::unordered_map() 函數形式 std::unordered_map 頭文件的聲明。

C++11

unordered_map(unordered_map&& um);

參數

um- 另一個相同類型的 unordered_map 對象。

返回值

構造函數從不返回值。

時間複雜度

常數,即 O(1)

示例

下麵的例子展示了 std::unordered_map::unordered_map() 函數的用法。

#include <iostream>
#include <unordered_map>

using namespace std;

int main(void) {
   unordered_map<char, int> um1 = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5}
            };

   unordered_map<char, int>um2(move(um1));

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

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

   return 0;
}

讓我們編譯並運行上麵的程序,這將產生以下結果——

Unordered_map contains following elements
e = 5
a = 1
b = 2
c = 3
d = 4

相關用法


注:本文由純淨天空篩選整理自 C++ Unordered_map Library - unordered_map() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。