C++ Unordered_map 库 - unordered_map() 函数
描述
C++ 函数 std::unordered_map::unordered_map() 构造一个 unordered_map,其元素数量在 first 到 last 的范围内。
声明
以下是 std::unordered_map::unordered_map() 函数形式 std::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

