目录

unordered_map::unordered_map

描述 (Description)

C ++函数std::unordered_map::unordered_map()从initialize list构造一个unordered_map。

声明 (Declaration)

以下是std :: unordered_map :: unordered_map()函数形式std :: unordered_map头的声明。

C++11

unordered_map(initializer_list<value_type> il,
              size_type n = /* Implementation defined */,
              const hasher& hf = hasher(),
              const key_equal& eql = key_equal(),
              const allocator_type& alloc = allocator_type()
              );

参数 (Parameters)

  • il - 初始化列表对象。

  • n - 初始存储桶的最大数量。

  • hf - 要使用的哈希函数。

  • eql - 比较函数对象,如果提供两个容器对象,则返回true。

  • alloc - 用于此容器的所有内存分配的分配器。

返回值

构造函数永远不会返回值。

时间复杂

线性即平均情况下为O(n)。

在最坏的情况下,二次即O(n 2 )。

例子 (Example)

以下示例显示了std :: unordered_map :: unordered_map()函数的用法。

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_map<char, int> um {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5}
            };
   cout << "Unordered map contains following elements" << endl;
   for (auto it = um.begin(); it != um.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
↑回到顶部↑
WIKI教程 @2018