(constructor)
描述 (Description)
它用于构造unordered_set容器对象。
声明 (Declaration)
以下是std :: unordered_set :: unordered_set的声明。
C++98
explicit unordered_set ( size_type n = /* see below */,
const hasher& hf = hasher(),
const key_equal& eql = key_equal(),
const allocator_type& alloc = allocator_type() );
参数 (Parameters)
n - 它包含有关初始存储桶最小数量的信息。
hf - 它是一个哈希函数对象。
eql - 它是一个比较函数对象。
alloc - 它是一个allowcator对象。
first, last - 输入迭代器。
ust - 它是另一个相同类型的unordered_set对象。
il - 它是一个initializer_list对象。
返回值
没有
异常 (Exceptions)
如果任何元素比较对象抛出异常,则抛出异常。
请注意,无效参数会导致未定义的行为。
时间复杂
恒定时间。
例子 (Example)
以下示例显示了std :: unordered_set :: unordered_set的用法。
#include <iostream>
#include <string>
#include <unordered_set>
template<class T>
T cmerge (T a, T b) { T t(a); t.insert(b.begin(),b.end()); return t; }
int main () {
std::unordered_set<std::string> first;
std::unordered_set<std::string> second ( {"100","200","300"} );
std::unordered_set<std::string> third ( {"400","500","600"} );
std::unordered_set<std::string> fourth ( second );
std::unordered_set<std::string> fifth ( cmerge(third,fourth) );
std::unordered_set<std::string> sixth ( fifth.begin(), fifth.end() );
std::cout << "sixth contains:";
for (const std::string& x: sixth) std::cout << " " << x;
std::cout << std::endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
sixth contains: 500 400 300 600 100 200