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



描述

C++ 函式std::unordered_map::unordered_map() 構造一個空的無序對映,其中包含零個元素。

宣告

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

C++11

explicit unordered_map(size_type n = /* implementation defined */,
                       const hasher& hf = hasher(),
                       const key_equal& eql = key_equal(),
                       const allocator_type& alloc = allocator_type()
                      );

引數

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

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

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

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

返回值

建構函式不返回值。

時間複雜度

常數,即 O(1)

示例

以下示例演示了 std::unordered_map::unordered_map() 函式的使用。

#include <iostream>
#include <unordered_map>

using namespace std;

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

   cout << "Size of unordered_map = " << um.size() << endl;

   return 0;
}

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

Size of unordered_map = 0
unordered_map.htm
廣告