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()
             );

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

参数

  • 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
广告