C++ 無序對映庫 - unordered_map() 函式



描述

C++ 函式 std::unordered_map::unordered_map() 使用範圍內的元素構造一個無序對映。開始結束.

宣告

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

C++11

unordered_map(InputIterator first, InputIterator last,
              size_type n = /* Implementation defined */,
              const hasher& hf = hasher(),
              const key_equal& eql = key_equal(),
              const allocator_type& alloc = allocator_type()
             );

引數

  • first − 輸入迭代器指向初始位置。

  • last − 輸入迭代器指向最終位置。

  • n − 初始桶的最大數量。

  • hf − 要使用的雜湊函式。

  • eql − 比較函式物件,如果提供的兩個容器物件被認為相等則返回 true。

  • alloc − 用於此容器所有記憶體分配的分配器。

返回值

建構函式永不返回值。

時間複雜度

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

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

示例

以下示例顯示了 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(um1.begin(), um2.end());

   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
a = 1
b = 2
c = 3
d = 4
e = 5
unordered_map.htm
廣告