此类模板描述用于控制 const Key 类型的变长元素序列的对象。 序列由哈希函数弱排序,哈希函数将此序列分区到称为存储桶的有序序列集中。 在每个存储桶中,比较函数将确定任一元素对是否具有等效顺序。 每个元素同时用作排序键和值。 序列以允许查找、插入和移除任意元素的方式表示,并包含与序列中的元素数量无关的多个操作(常量时间),至少在所有存储桶长度大致相等时如此。 在最坏情况下,当所有元素位于一个存储桶中时,操作数量与序列中的元素数量成比例(线性时间)。 插入元素不会使迭代器失效,移除元素仅会使指向已移除元素的迭代器失效。
语法
template <
   class Key,
   class Hash = std::hash<Key>,
   class Pred = std::equal_to<Key>,
   class Alloc = std::allocator<Key>>
class unordered_set;
参数
Key
键类型。
Hash
哈希函数对象类型。
Pred
相等比较函数对象类型。
Alloc
allocator 类。
成员
Typedef
| 名称 | 描述 | 
|---|---|
| allocator_type | 用于管理存储的分配器的类型。 | 
| const_iterator | 受控序列的常量迭代器的类型。 | 
| const_local_iterator | 受控序列的常量存储桶迭代器的类型。 | 
| const_pointer | 元素的常量指针的类型。 | 
| const_reference | 元素的常量引用的类型。 | 
| difference_type | 两个元素间的带符号距离的类型。 | 
| hasher | 哈希函数的类型。 | 
| iterator | 受控序列的迭代器的类型。 | 
| key_equal | 比较函数的类型。 | 
| key_type | 排序键的类型。 | 
| local_iterator | 受控序列的存储桶迭代器的类型。 | 
| pointer | 指向元素的指针的类型。 | 
| reference | 元素的引用的类型。 | 
| size_type | 两个元素间的无符号距离的类型。 | 
| value_type | 元素的类型。 | 
Functions
| 名称 | 描述 | 
|---|---|
| begin | 指定受控序列的开头。 | 
| bucket | 获取键值的存储桶编号。 | 
| bucket_count | 获取存储桶数。 | 
| bucket_size | 获取存储桶的大小。 | 
| cbegin | 指定受控序列的开头。 | 
| cend | 指定受控序列的末尾。 | 
| clear | 删除所有元素。 | 
| containsC++20 | 检查 unordered_set中是否包含具有指定键的元素。 | 
| count | 查找与指定键匹配的元素数。 | 
| emplace | 添加就地构造的元素。 | 
| emplace_hint | 添加就地构造的元素,附带提示。 | 
| empty | 测试元素是否存在。 | 
| end | 指定受控序列的末尾。 | 
| equal_range | 查找与指定键匹配的范围。 | 
| erase | 移除指定位置处的元素。 | 
| find | 查找与指定键匹配的元素。 | 
| get_allocator | 获取存储的分配器对象。 | 
| hash_function | 获取存储的哈希函数对象。 | 
| insert | 添加元素。 | 
| key_eq | 获取存储的比较函数对象。 | 
| load_factor | 对每个存储桶的平均元素数进行计数。 | 
| max_bucket_count | 获取最大的存储桶数。 | 
| max_load_factor | 获取或设置每个存储桶的最多元素数。 | 
| max_size | 获取受控序列的最大大小。 | 
| rehash | 重新生成哈希表。 | 
| size | 对元素数进行计数。 | 
| swap | 交换两个容器的内容。 | 
| unordered_set | 构造容器对象。 | 
运算符
| 名称 | 描述 | 
|---|---|
| unordered_set::operator= | 复制哈希表。 | 
备注
对象通过调用两个存储对象,即一个 unordered_set::key_equal 类型的比较函数对象和一个 unordered_set::hasher 类型的哈希函数对象,对它控制的序列进行排序。 可以通过调用成员函数 unordered_set::key_eq() 访问第一个存储对象;通过调用成员函数 unordered_set::hash_function() 访问第二个存储对象。 具体而言,对于所有 X 类型的值 Y 和 Key,key_eq()(X, Y) 调用将仅在两个参数值拥有等效顺序时返回 true;hash_function()(keyval) 调用将生成 size_t 类型的值的分布。 与类模板 unordered_multiset不同,类型的 unordered_set 对象可确保 key_eq()(X, Y) 受控序列的任何两个元素始终为 false。 (键是唯一的。)
此对象还存储最大加载因子,用于指定每个存储桶的元素的最大所需平均数量。 如果插入元素导致 unordered_set::load_factor() 超出最大加载因子,容器将增加存储桶的数量并根据需要重新生成哈希表。
受控序列中元素的实际顺序取决于哈希函数、比较函数、插入顺序、最大加载因子和存储桶的当前数量。 通常无法预测受控序列中的元素顺序。 但是,可以始终确保具有等效顺序的任何元素子集在受控序列中相邻。
对象通过 unordered_set::allocator_type 类型的存储分配器对象为其控制的序列分配并释放存储。 此分配器对象必须与 allocator 类型的对象的外部接口相同。 分配容器对象时,不会复制存储的分配器对象。
 unordered_set::allocator_type
用于管理存储的分配器的类型。
typedef Alloc allocator_type;
备注
该类型是模板参数 Alloc 的同义词。
示例
// std__unordered_set__unordered_set_allocator_type.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
typedef std::allocator<std::pair<const char, int> > Myalloc;
int main()
{
    Myset c1;
    Myset::allocator_type al = c1.get_allocator();
    std::cout << "al == std::allocator() is "
    << std::boolalpha << (al == Myalloc()) << std::endl;
    return (0);
}
al == std::allocator() is true
 begin
指定受控序列或存储桶的开头。
iterator begin();
const_iterator begin() const;
local_iterator begin(size_type nbucket);
const_local_iterator begin(size_type nbucket) const;
参数
nbucket
存储桶编号。
备注
前两个编号函数返回向前迭代器,指向序列的第一个元素(或紧邻空序列后的位置)。 最后两个成员函数返回一个向前迭代器,指向存储桶 nbucket 的第一个元素(或刚超出空存储桶末尾的位置)。
示例
// unordered_set_begin.cpp
// compile using: cl.exe /EHsc /nologo /W4 /MTd
#include <unordered_set>
#include <iostream>
using namespace std;
typedef unordered_set<char> MySet;
int main()
{
    MySet c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents using range-based for
    for (auto it : c1) {
    cout << "[" << it << "] ";
    }
    cout << endl;
    // display contents using explicit for
    for (MySet::const_iterator it = c1.begin(); it != c1.end(); ++it) {
        cout << "[" << *it << "] ";
    }
    cout << std::endl;
    // display first two items
    MySet::iterator it2 = c1.begin();
    cout << "[" << *it2 << "] ";
    ++it2;
    cout << "[" << *it2 << "] ";
    cout << endl;
    // display bucket containing 'a'
    MySet::const_local_iterator lit = c1.begin(c1.bucket('a'));
    cout << "[" << *lit << "] ";
    return (0);
}
[a] [b] [c]
[a] [b] [c]
[a] [b]
[a]
 bucket
获取键值的存储桶编号。
size_type bucket(const Key& keyval) const;
参数
keyval
要映射的键值。
备注
成员函数返回当前与键值 keyval对应的存储桶编号。
示例
// std__unordered_set__unordered_set_bucket.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // display buckets for keys
    Myset::size_type bs = c1.bucket('a');
    std::cout << "bucket('a') == " << bs << std::endl;
    std::cout << "bucket_size(" << bs << ") == " << c1.bucket_size(bs)
    << std::endl;
    return (0);
}
[c] [b] [a]
bucket('a') == 7
bucket_size(7) == 1
 bucket_count
获取存储桶数。
size_type bucket_count() const;
备注
该成员函数将返回存储桶的当前数量。
示例
// std__unordered_set__unordered_set_bucket_count.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // inspect current parameters
    std::cout << "bucket_count() == " << c1.bucket_count() << std::endl;
    std::cout << "load_factor() == " << c1.load_factor() << std::endl;
    std::cout << "max_bucket_count() == "
    << c1.max_bucket_count() << std::endl;
    std::cout << "max_load_factor() == "
    << c1.max_load_factor() << std::endl;
    std::cout << std::endl;
    // change max_load_factor and redisplay
    c1.max_load_factor(0.10f);
    std::cout << "bucket_count() == " << c1.bucket_count() << std::endl;
    std::cout << "load_factor() == " << c1.load_factor() << std::endl;
    std::cout << "max_bucket_count() == "
    << c1.max_bucket_count() << std::endl;
    std::cout << "max_load_factor() == "
    << c1.max_load_factor() << std::endl;
    std::cout << std::endl;
    // rehash and redisplay
    c1.rehash(100);
    std::cout << "bucket_count() == " << c1.bucket_count() << std::endl;
    std::cout << "load_factor() == " << c1.load_factor() << std::endl;
    std::cout << "max_bucket_count() == "
    << c1.max_bucket_count() << std::endl;
    std::cout << "max_load_factor() == "
    << c1.max_load_factor() << std::endl;
    std::cout << std::endl;
    return (0);
}
[c] [b] [a]
bucket_count() == 8
load_factor() == 0.375
max_bucket_count() == 8
max_load_factor() == 4
bucket_count() == 8
load_factor() == 0.375
max_bucket_count() == 8
max_load_factor() == 0.1
bucket_count() == 128
load_factor() == 0.0234375
max_bucket_count() == 128
max_load_factor() == 0.1
 bucket_size
获取存储桶的大小
size_type bucket_size(size_type nbucket) const;
参数
nbucket
存储桶编号。
备注
成员函数返回编号为 nbucket的存储桶的大小。
示例
// std__unordered_set__unordered_set_bucket_size.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // display buckets for keys
    Myset::size_type bs = c1.bucket('a');
    std::cout << "bucket('a') == " << bs << std::endl;
    std::cout << "bucket_size(" << bs << ") == " << c1.bucket_size(bs)
    << std::endl;
    return (0);
}
[c] [b] [a]
bucket('a') == 7
bucket_size(7) == 1
 cbegin
返回确定范围中第一个元素地址的 const 迭代器。
const_iterator cbegin() const;
返回值
              const 前向访问迭代器,指向范围的第一个元素,或刚超出空范围末尾的位置(对于空范围,cbegin() == cend())。
备注
由于使用 cbegin 的返回值,因此不能修改范围中的元素。
可以使用此成员函数替代 begin() 成员函数,以保证返回值为 const_iterator。 它一般与 auto 类型推导关键字联合使用,如以下示例所示。 在此示例中,将 Container 视为支持 const 和 begin() 的可修改的任何类型的(非- cbegin())容器。
auto i1 = Container.begin();
// i1 isContainer<T>::iterator
auto i2 = Container.cbegin();
// i2 isContainer<T>::const_iterator
 cend
返回一个 const 迭代器,此迭代器用于发现刚超出范围中最后一个元素的位置。
const_iterator cend() const;
返回值
指向刚超出范围末尾的位置的 const 向前访问迭代器。
备注
              cend 用于测试迭代器是否超过了其范围的末尾。
可以使用此成员函数替代 end() 成员函数,以保证返回值为 const_iterator。 它一般与 auto 类型推导关键字联合使用,如以下示例所示。 在此示例中,将 Container 视为支持 const 和 end() 的可修改的任何类型的(非- cend())容器。
auto i1 = Container.end();
// i1 isContainer<T>::iterator
auto i2 = Container.cend();
// i2 isContainer<T>::const_iterator
不应对 cend 返回的值取消引用。
 clear
删除所有元素。
void clear();
备注
此成员函数调用 unordered_set::erase( unordered_set::begin()、unordered_set::end())。 有关详细信息,请参阅unordered_set::erase、unordered_set::begin和unordered_set::end。
示例
// std__unordered_set__unordered_set_clear.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // clear the container and reinspect
    c1.clear();
    std::cout << "size == " << c1.size() << std::endl;
    std::cout << "empty() == " << std::boolalpha << c1.empty() << std::endl;
    std::cout << std::endl;
    c1.insert('d');
    c1.insert('e');
    // display contents "[e] [d] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    std::cout << "size == " << c1.size() << std::endl;
    std::cout << "empty() == " << std::boolalpha << c1.empty() << std::endl;
    return (0);
}
[c] [b] [a]
size == 0
empty() == true
[e] [d]
size == 2
empty() == false
 const_iterator
受控序列的常量迭代器的类型。
typedef T1 const_iterator;
备注
此类型描述为可用作受控序列的常量向前迭代器的对象。 在此处描述为实现定义的 T1 类型的同义词。
示例
// std__unordered_set__unordered_set_const_iterator.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
    std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    return (0);
}
[c] [b] [a]
 const_local_iterator
受控序列的常量存储桶迭代器的类型。
typedef T5 const_local_iterator;
备注
该类型描述了可用作存储桶的常量向前迭代器的对象。 在此处描述为实现定义的 T5 类型的同义词。
示例
// std__unordered_set__unordered_set_const_local_iterator.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // inspect bucket containing 'a'
    Myset::const_local_iterator lit = c1.begin(c1.bucket('a'));
    std::cout << "[" << *lit << "] ";
    return (0);
}
[c] [b] [a]
[a]
 const_pointer
元素的常量指针的类型。
typedef Alloc::const_pointer const_pointer;
备注
该类型描述了可用作指向受控序列中元素的常量指针的对象。
示例
// std__unordered_set__unordered_set_const_pointer.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::iterator it = c1.begin(); it != c1.end(); ++it)
    {
        Myset::const_pointer p = &*it;
        std::cout << "[" << *p << "] ";
    }
    std::cout << std::endl;
    return (0);
}
[c] [b] [a]
 const_reference
元素的常量引用的类型。
typedef Alloc::const_reference const_reference;
备注
该类型将可作为常量引用的对象描述为受控序列中的元素。
示例
// std__unordered_set__unordered_set_const_reference.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::iterator it = c1.begin(); it != c1.end(); ++it)
    {
        Myset::const_reference ref = *it;
        std::cout << "[" << ref << "] ";
    }
    std::cout << std::endl;
    return (0);
}
[c] [b] [a]
 contains
检查 unordered_set 中是否包含具有指定键的元素。
bool contains(const Key& key) const;
template<class K> bool contains(const K& key) const;
参数
K
键的类型。
Key
要查找的元素的键值。
返回值
如果在容器中找到元素,则为 true;否则为 false。
备注
              contains() 是 C++20 中的新增功能。 若要使用它,请指定 /std:c++20 或更高版本编译器选项。
如果 template<class K> bool contains(const K& key) const 是透明的,则 key_compare 仅参与重载决策。
示例
// Requires /std:c++20 or later
#include <unordered_set>
#include <iostream>
int main()
{
    std::unordered_set<int> theUnorderedSet = { 1, 2 };
    std::cout << std::boolalpha; // so booleans show as 'true' or 'false'
    std::cout << theUnorderedSet.contains(2) << '\n';
    std::cout << theUnorderedSet.contains(3) << '\n';
    return 0;
}
true
false
 count
查找与指定键匹配的元素数。
size_type count(const Key& keyval) const;
参数
keyval
要搜索的键值。
备注
该成员函数返回由 unordered_set::equal_range(keyval) 分隔的范围内的元素数量。
示例
// std__unordered_set__unordered_set_count.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    std::cout << "count('A') == " << c1.count('A') << std::endl;
    std::cout << "count('b') == " << c1.count('b') << std::endl;
    std::cout << "count('C') == " << c1.count('C') << std::endl;
    return (0);
}
[c] [b] [a]
count('A') == 0
count('b') == 1
count('C') == 0
 difference_type
两个元素间的带符号距离的类型。
typedef T3 difference_type;
备注
带符号的整数类型描述一个可表示受控序列中任意两个元素的地址之间的差异的对象。 在此处描述为实现定义的 T3 类型的同义词。
示例
// std__unordered_set__unordered_set_difference_type.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // compute positive difference
    Myset::difference_type diff = 0;
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        ++diff;
    std::cout << "end()-begin() == " << diff << std::endl;
    // compute negative difference
    diff = 0;
    for (Myset::const_iterator it = c1.end(); it != c1.begin(); --it)
        --diff;
    std::cout << "begin()-end() == " << diff << std::endl;
    return (0);
}
[c] [b] [a]
end()-begin() == 3
begin()-end() == -3
 emplace
就地插入构造的元素(不执行复制或移动操作)。
template <class... Args>
pair<iterator, bool>
emplace(
Args&&... args);
参数
args
用于构造要插入到 unordered_set 中的元素的转发参数(除非它已包含一个具有相对有序的值的元素)。
返回值
如果完成插入操作,则包含 pair 组件的 bool 返回 true,如果 unordered_set 已包含一个值在排序中具有等效值的元素,则返回 false;此对的迭代器组件返回新元素的插入位置或已包含的元素的位置。
若要访问此成员函数返回的 pr 对的迭代器组件,请使用 pr.first;若要对其取消引用,请使用 *(pr.first)。 若要访问此成员函数返回的 bool 对的 pr 组件,请使用 pr.second。
备注
此函数不会使迭代器或引用无效。
在插入期间,如果引发了异常但未发生在容器的哈希函数中,则不会修改此容器。 如果在哈希函数中引发异常,则未定义此结果。
有关代码示例,请参阅 set::emplace。
 emplace_hint
使用位置提示就地插入构造的元素(不执行复制或移动操作)。
template <class... Args>
iterator emplace_hint(
const_iteratorwhere,
Args&&... args);
参数
args
用于构造要插入到 unordered_set 中的元素的转发参数(除非 unordered_set 已包含该元素,或更普遍的情况是,除非它已包含其键已经过相同排序的元素)。
where
有关开始搜索正确插入点的位置的提示。
返回值
指向新插入的元素的迭代器。
如果因元素已存在导致插入失败,则将迭代器返回现有元素。
备注
此函数不会使迭代器或引用无效。
在插入期间,如果引发了异常但未发生在容器的哈希函数中,则不会修改此容器。 如果在哈希函数中引发异常,则未定义此结果。
有关代码示例,请参阅 set::emplace_hint。
 empty
测试元素是否存在。
bool empty() const;
备注
对于空受控序列,该成员函数返回 true。
示例
// std__unordered_set__unordered_set_empty.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // clear the container and reinspect
    c1.clear();
    std::cout << "size == " << c1.size() << std::endl;
    std::cout << "empty() == " << std::boolalpha << c1.empty() << std::endl;
    std::cout << std::endl;
    c1.insert('d');
    c1.insert('e');
    // display contents "[e] [d] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    std::cout << "size == " << c1.size() << std::endl;
    std::cout << "empty() == " << std::boolalpha << c1.empty() << std::endl;
    return (0);
}
[c] [b] [a]
size == 0
empty() == true
[e] [d]
size == 2
empty() == false
 end
指定受控序列的末尾。
iterator end();
const_iterator end() const;
local_iterator end(size_type nbucket);
const_local_iterator end(size_type nbucket) const;
参数
nbucket
存储桶编号。
备注
前两个成员函数返回一个向前迭代器,它指向刚超出序列末尾的位置。 最后两个成员函数返回一个向前迭代器,它指向刚超出存储桶 nbucket 末尾的位置。
示例
// std__unordered_set__unordered_set_end.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // inspect last two items "[a] [b] "
    Myset::iterator it2 = c1.end();
    --it2;
    std::cout << "[" << *it2 << "] ";
    --it2;
    std::cout << "[" << *it2 << "] ";
    std::cout << std::endl;
    // inspect bucket containing 'a'
    Myset::const_local_iterator lit = c1.end(c1.bucket('a'));
    --lit;
    std::cout << "[" << *lit << "] ";
    return (0);
}
[c] [b] [a]
[a] [b]
[a]
 equal_range
查找与指定键匹配的范围。
std::pair<iterator, iterator>
equal_range(const Key& keyval);
std::pair<const_iterator, const_iterator>
equal_range(const Key& keyval) const;
参数
keyval
要搜索的键值。
备注
成员函数返回一对迭代器 X ,以便 [X.first, X.second) 仅分隔受控序列中与 keyval具有等效排序的那些元素。 如果不存在此类元素,则两个迭代器均为 end()。
示例
// std__unordered_set__unordered_set_equal_range.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // display results of failed search
    std::pair<Myset::iterator, Myset::iterator> pair1 =
    c1.equal_range('x');
    std::cout << "equal_range('x'):";
    for (; pair1.first != pair1.second; ++pair1.first)
        std::cout << "[" << *pair1.first << "] ";
    std::cout << std::endl;
    // display results of successful search
    pair1 = c1.equal_range('b');
    std::cout << "equal_range('b'):";
    for (; pair1.first != pair1.second; ++pair1.first)
        std::cout << "[" << *pair1.first << "] ";
    std::cout << std::endl;
    return (0);
}
[c] [b] [a]
equal_range('x'):
equal_range('b'): [b]
 erase
从 unordered_set 中的指定位置移除一个元素或元素范围,或者移除与指定键匹配的元素。
iterator erase(const_iterator Where);
iterator erase(const_iterator First, const_iterator Last);
size_type erase(const key_type& Key);
参数
Where
要移除的元素的位置。
First
要移除的第一个元素的位置。
Last
要移除的刚超出最后一个元素的位置。
Key
要移除的元素的关键值。
返回值
对于前两个成员函数,则为双向迭代器,它指定已删除的任何元素之外留存的第一个元素,如果此类元素不存在,则为 unordered_set 末尾的元素。
对于第三个成员函数,则返回已从 unordered_set 中移除的元素的数目。
备注
有关代码示例,请参阅 set::erase。
 find
查找与指定键匹配的元素。
const_iterator find(const Key& keyval) const;
参数
keyval
要搜索的键值。
备注
成员函数返回 unordered_set::equal_range(keyval).first。
示例
// std__unordered_set__unordered_set_find.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // try to find and fail
    std::cout << "find('A') == "
    << std::boolalpha << (c1.find('A') != c1.end()) << std::endl;
    // try to find and succeed
    Myset::iterator it = c1.find('b');
    std::cout << "find('b') == "
    << std::boolalpha << (it != c1.end())
    << ": [" << *it << "] " << std::endl;
    return (0);
}
[c] [b] [a]
find('A') == false
find('b') == true: [b]
 get_allocator
获取存储的分配器对象。
Alloc get_allocator() const;
备注
该成员函数将返回存储的分配器对象。
示例
// std__unordered_set__unordered_set_get_allocator.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
typedef std::allocator<std::pair<const char, int> > Myalloc;
int main()
{
    Myset c1;
    Myset::allocator_type al = c1.get_allocator();
    std::cout << "al == std::allocator() is "
    << std::boolalpha << (al == Myalloc()) << std::endl;
    return (0);
}
al == std::allocator() is true
 hash_function
获取存储的哈希函数对象。
Hash hash_function() const;
备注
成员函数将返回存储的哈希函数对象。
示例
// std__unordered_set__unordered_set_hash_function.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    Myset::hasher hfn = c1.hash_function();
    std::cout << "hfn('a') == " << hfn('a') << std::endl;
    std::cout << "hfn('b') == " << hfn('b') << std::endl;
    return (0);
}
hfn('a') == 1630279
hfn('b') == 1647086
 hasher
哈希函数的类型。
typedef Hash hasher;
备注
该类型是模板参数 Hash 的同义词。
示例
// std__unordered_set__unordered_set_hasher.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    Myset::hasher hfn = c1.hash_function();
    std::cout << "hfn('a') == " << hfn('a') << std::endl;
    std::cout << "hfn('b') == " << hfn('b') << std::endl;
    return (0);
}
hfn('a') == 1630279
hfn('b') == 1647086
 insert
将一个元素或元素范围插入到 unordered_set 中。
// (1) single element
pair<iterator, bool> insert(const value_type& Val);
// (2) single element, perfect forwarded
template <class ValTy>
pair<iterator, bool> insert(ValTy&& Val);
// (3) single element with hint
iterator insert(const_iterator Where, const value_type& Val);
// (4) single element, perfect forwarded, with hint
template <class ValTy>
iterator insert(const_iterator Where, ValTy&& Val);
// (5) range
template <class InputIterator>
void insert(InputIterator First, InputIterator Last);
// (6) initializer list
void insert(initializer_list<value_type> IList);
参数
Val
要插入到 unordered_set 中的元素的值(除非它已经包含一个具有相对有序的键的元素)。
Where
开始搜索正确插入点的位置。
ValTy
指定 unordered_set 可用于构造 value_type 元素的自变量类型并将 Val 作为自变量完美转发的模板参数。
First
要复制的第一个元素的位置。
Last
要复制的最后一个元素以外的位置。
InputIterator
满足输入迭代器需求的模板函数参数,该输入迭代器指向可用于构造 value_type 对象的类型的元素。
IList
要从中复制元素的 initializer_list。
返回值
单元素成员函数 (1) 和 (2) 将返回 pair,如果完成插入,则其 bool 组件为 true;如果 unordered_set 已经包含一个其键在排序中具有等效值的元素,则为 false。 返回值对的迭代器组件将指向新插入的元素(如果 bool 组件为 true)或现有元素(如果 bool 组件为 false)。
附带提示的单元素成员函数 (3) 和 (4) 将返回迭代器,该迭代器指向将新元素插入到 unordered_set 中的位置,如果具有等效键的元素已经存在,则指向现有元素。
备注
任何迭代器、指针或引用都不会因为此函数而失效。
在只插入单个元素的过程中,如果引发异常,但是异常并未在容器的哈希函数中发生,则不会修改该容器的状态。 如果在哈希函数中引发异常,则未定义此结果。 在插入多个元素的过程中,如果引发异常,则会使容器处于未指定但有效的状态。
若要访问由单元素成员函数返回的pairpr迭代器组件,请使用 pr.first;在返回的对中取消引用迭代器,使用*pr.first,从而提供一个元素。 要访问 bool 组件,请使用 pr.second。 有关示例,请参阅本文后面的示例代码。
容器的 value_type 是属于该容器的 typedef;对于集,unordered_set<V>::value_type 是类型 const V。
范围成员函数 (5) 将元素值序列插入到 unordered_set 中,它对应于迭代器在范围 [First, Last) 中所处理的每一个元素;因此,不会插入 Last。 容器成员函数 end() 是指容器中最后一个元素之后的位置,例如,s.insert(v.begin(), v.end()); 语句尝试将 v 的所有元素插入到 s 中。 只插入在该范围中具有唯一值的元素;忽略副本。 若要观察拒绝了哪些元素,请使用单个元素版本的 insert。
初始值设定项列表成员函数 (6) 使用 initializer_list 将元素复制到 unordered_set 中。
有关就地构造的元素的插入(即不会执行复制或移动操作),请参阅 set::emplace 和 set::emplace_hint。
有关代码示例,请参阅 set::insert。
 iterator
一种类型,此类型提供可读取 unordered_set 中的元素的向前迭代器。
typedef implementation-defined iterator;
示例
有关如何声明和使用 iteratorbegin 的示例,请参阅  的示例。
 key_eq
获取存储的比较函数对象。
Pred key_eq() const;
备注
成员函数将返回存储的比较函数对象。
示例
// std__unordered_set__unordered_set_key_eq.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    Myset::key_equal cmpfn = c1.key_eq();
    std::cout << "cmpfn('a', 'a') == "
    << std::boolalpha << cmpfn('a', 'a') << std::endl;
    std::cout << "cmpfn('a', 'b') == "
    << std::boolalpha << cmpfn('a', 'b') << std::endl;
    return (0);
}
cmpfn('a', 'a') == true
cmpfn('a', 'b') == false
 key_equal
比较函数的类型。
typedef Pred key_equal;
备注
该类型是模板参数 Pred 的同义词。
示例
// std__unordered_set__unordered_set_key_equal.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    Myset::key_equal cmpfn = c1.key_eq();
    std::cout << "cmpfn('a', 'a') == "
    << std::boolalpha << cmpfn('a', 'a') << std::endl;
    std::cout << "cmpfn('a', 'b') == "
    << std::boolalpha << cmpfn('a', 'b') << std::endl;
    return (0);
}
cmpfn('a', 'a') == true
cmpfn('a', 'b') == false
 key_type
排序键的类型。
typedef Key key_type;
备注
该类型是模板参数 Key 的同义词。
示例
// std__unordered_set__unordered_set_key_type.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // add a value and reinspect
    Myset::key_type key = 'd';
    Myset::value_type val = key;
    c1.insert(val);
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    return (0);
}
[c] [b] [a]
[d] [c] [b] [a]
 load_factor
对每个存储桶的平均元素数进行计数。
float load_factor() const;
备注
成员函数返回每个存储桶的平均元素数 (float)unordered_set::size() / (float)unordered_set::bucket_count()。
示例
// std__unordered_set__unordered_set_load_factor.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // inspect current parameters
    std::cout << "bucket_count() == " << c1.bucket_count() << std::endl;
    std::cout << "load_factor() == " << c1.load_factor() << std::endl;
    std::cout << "max_bucket_count() == "
    << c1.max_bucket_count() << std::endl;
    std::cout << "max_load_factor() == "
    << c1.max_load_factor() << std::endl;
    std::cout << std::endl;
    // change max_load_factor and redisplay
    c1.max_load_factor(0.10f);
    std::cout << "bucket_count() == " << c1.bucket_count() << std::endl;
    std::cout << "load_factor() == " << c1.load_factor() << std::endl;
    std::cout << "max_bucket_count() == "
    << c1.max_bucket_count() << std::endl;
    std::cout << "max_load_factor() == "
    << c1.max_load_factor() << std::endl;
    std::cout << std::endl;
    // rehash and redisplay
    c1.rehash(100);
    std::cout << "bucket_count() == " << c1.bucket_count() << std::endl;
    std::cout << "load_factor() == " << c1.load_factor() << std::endl;
    std::cout << "max_bucket_count() == "
    << c1.max_bucket_count() << std::endl;
    std::cout << "max_load_factor() == "
    << c1.max_load_factor() << std::endl;
    std::cout << std::endl;
    return (0);
}
[c] [b] [a]
bucket_count() == 8
load_factor() == 0.375
max_bucket_count() == 8
max_load_factor() == 4
bucket_count() == 8
load_factor() == 0.375
max_bucket_count() == 8
max_load_factor() == 0.1
bucket_count() == 128
load_factor() == 0.0234375
max_bucket_count() == 128
max_load_factor() == 0.1
 local_iterator
存储桶迭代器类型。
typedef T4 local_iterator;
备注
此类型描述可用作存储桶的向前迭代器的对象。 在此处描述为实现定义的 T4 类型的同义词。
示例
// std__unordered_set__unordered_set_local_iterator.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // inspect bucket containing 'a'
    Myset::local_iterator lit = c1.begin(c1.bucket('a'));
    std::cout << "[" << *lit << "] ";
    return (0);
}
[c] [b] [a]
[a]
 max_bucket_count
获取最大的存储桶数。
size_type max_bucket_count() const;
备注
该成员函数将返回当前允许的最大存储桶数。
示例
// std__unordered_set__unordered_set_max_bucket_count.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // inspect current parameters
    std::cout << "bucket_count() == " << c1.bucket_count() << std::endl;
    std::cout << "load_factor() == " << c1.load_factor() << std::endl;
    std::cout << "max_bucket_count() == "
    << c1.max_bucket_count() << std::endl;
    std::cout << "max_load_factor() == "
    << c1.max_load_factor() << std::endl;
    std::cout << std::endl;
    // change max_load_factor and redisplay
    c1.max_load_factor(0.10f);
    std::cout << "bucket_count() == " << c1.bucket_count() << std::endl;
    std::cout << "load_factor() == " << c1.load_factor() << std::endl;
    std::cout << "max_bucket_count() == "
    << c1.max_bucket_count() << std::endl;
    std::cout << "max_load_factor() == "
    << c1.max_load_factor() << std::endl;
    std::cout << std::endl;
    // rehash and redisplay
    c1.rehash(100);
    std::cout << "bucket_count() == " << c1.bucket_count() << std::endl;
    std::cout << "load_factor() == " << c1.load_factor() << std::endl;
    std::cout << "max_bucket_count() == "
    << c1.max_bucket_count() << std::endl;
    std::cout << "max_load_factor() == "
    << c1.max_load_factor() << std::endl;
    std::cout << std::endl;
    return (0);
}
[c] [b] [a]
bucket_count() == 8
load_factor() == 0.375
max_bucket_count() == 8
max_load_factor() == 4
bucket_count() == 8
load_factor() == 0.375
max_bucket_count() == 8
max_load_factor() == 0.1
bucket_count() == 128
load_factor() == 0.0234375
max_bucket_count() == 128
max_load_factor() == 0.1
 max_load_factor
获取或设置每个存储桶的最多元素数。
float max_load_factor() const;
void max_load_factor(float factor);
参数
factor
新的最大加载因子。
备注
第一个成员函数将返回存储的最大加载因子。 第二个成员函数将用 factor替换已存储的最大加载因子。
示例
// std__unordered_set__unordered_set_max_load_factor.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // inspect current parameters
    std::cout << "bucket_count() == " << c1.bucket_count() << std::endl;
    std::cout << "load_factor() == " << c1.load_factor() << std::endl;
    std::cout << "max_bucket_count() == "
    << c1.max_bucket_count() << std::endl;
    std::cout << "max_load_factor() == "
    << c1.max_load_factor() << std::endl;
    std::cout << std::endl;
    // change max_load_factor and redisplay
    c1.max_load_factor(0.10f);
    std::cout << "bucket_count() == " << c1.bucket_count() << std::endl;
    std::cout << "load_factor() == " << c1.load_factor() << std::endl;
    std::cout << "max_bucket_count() == "
    << c1.max_bucket_count() << std::endl;
    std::cout << "max_load_factor() == "
    << c1.max_load_factor() << std::endl;
    std::cout << std::endl;
    // rehash and redisplay
    c1.rehash(100);
    std::cout << "bucket_count() == " << c1.bucket_count() << std::endl;
    std::cout << "load_factor() == " << c1.load_factor() << std::endl;
    std::cout << "max_bucket_count() == "
    << c1.max_bucket_count() << std::endl;
    std::cout << "max_load_factor() == "
    << c1.max_load_factor() << std::endl;
    std::cout << std::endl;
    return (0);
}
[c] [b] [a]
bucket_count() == 8
load_factor() == 0.375
max_bucket_count() == 8
max_load_factor() == 4
bucket_count() == 8
load_factor() == 0.375
max_bucket_count() == 8
max_load_factor() == 0.1
bucket_count() == 128
load_factor() == 0.0234375
max_bucket_count() == 128
max_load_factor() == 0.1
 max_size
获取受控序列的最大大小。
size_type max_size() const;
备注
该成员函数将返回对象可控制的最长序列的长度。
示例
// std__unordered_set__unordered_set_max_size.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    std::cout << "max_size() == " << c1.max_size() << std::endl;
    return (0);
}
max_size() == 4294967295
 operator=
复制哈希表。
unordered_set& operator=(const unordered_set& right);
unordered_set& operator=(unordered_set&& right);
参数
right
要复制到 unordered_set 中的 unordered_set。
备注
清除 unordered_set中的任何现有元素后, operator= 会将 right 的内容复制或移动到 unordered_set。
示例
// unordered_set_operator_as.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
int main( )
{
    using namespace std;
    unordered_set<int> v1, v2, v3;
    unordered_set<int>::iterator iter;
    v1.insert(10);
    cout << "v1 = " ;
    for (iter = v1.begin(); iter != v1.end(); iter++)
        cout << *iter << " ";
    cout << endl;
    v2 = v1;
    cout << "v2 = ";
    for (iter = v2.begin(); iter != v2.end(); iter++)
        cout << *iter << " ";
    cout << endl;
    // move v1 into v2
    v2.clear();
    v2 = move(v1);
    cout << "v2 = ";
    for (iter = v2.begin(); iter != v2.end(); iter++)
        cout << *iter << " ";
    cout << endl;
}
 pointer
指向元素的指针的类型。
typedef Alloc::pointer pointer;
备注
该类型描述了可用作指向受控序列中元素的指针的对象。
示例
// std__unordered_set__unordered_set_pointer.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::iterator it = c1.begin(); it != c1.end(); ++it)
    {
        Myset::key_type key = *it;
        Myset::pointer p = &key;
        std::cout << "[" << *p << "] ";
    }
    std::cout << std::endl;
    return (0);
}
[c] [b] [a]
 reference
元素的引用的类型。
typedef Alloc::reference reference;
备注
该类型描述了可用作对受控序列中元素的引用的对象。
示例
// std__unordered_set__unordered_set_reference.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::iterator it = c1.begin(); it != c1.end(); ++it)
    {
        Myset::key_type key = *it;
        Myset::reference ref = key;
        std::cout << "[" << ref << "] ";
    }
    std::cout << std::endl;
    return (0);
}
[c] [b] [a]
 rehash
重新生成哈希表。
void rehash(size_type nbuckets);
参数
nbuckets
请求的存储桶数。
备注
成员函数将存储桶数更改为至少 nbuckets 并根据需要重新生成哈希表。
示例
// std__unordered_set__unordered_set_rehash.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // inspect current parameters
    std::cout << "bucket_count() == " << c1.bucket_count() << std::endl;
    std::cout << "load_factor() == " << c1.load_factor() << std::endl;
    std::cout << "max_load_factor() == " << c1.max_load_factor() << std::endl;
    std::cout << std::endl;
    // change max_load_factor and redisplay
    c1.max_load_factor(0.10f);
    std::cout << "bucket_count() == " << c1.bucket_count() << std::endl;
    std::cout << "load_factor() == " << c1.load_factor() << std::endl;
    std::cout << "max_load_factor() == " << c1.max_load_factor() << std::endl;
    std::cout << std::endl;
    // rehash and redisplay
    c1.rehash(100);
    std::cout << "bucket_count() == " << c1.bucket_count() << std::endl;
    std::cout << "load_factor() == " << c1.load_factor() << std::endl;
    std::cout << "max_load_factor() == " << c1.max_load_factor() << std::endl;
    return (0);
}
[c] [b] [a]
bucket_count() == 8
load_factor() == 0.375
max_load_factor() == 4
bucket_count() == 8
load_factor() == 0.375
max_load_factor() == 0.1
bucket_count() == 128
load_factor() == 0.0234375
max_load_factor() == 0.1
 size
对元素数进行计数。
size_type size() const;
备注
成员函数将返回受控序列的长度。
示例
// std__unordered_set__unordered_set_size.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // clear the container and reinspect
    c1.clear();
    std::cout << "size == " << c1.size() << std::endl;
    std::cout << "empty() == " << std::boolalpha << c1.empty() << std::endl;
    std::cout << std::endl;
    c1.insert('d');
    c1.insert('e');
    // display contents "[e] [d] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    std::cout << "size == " << c1.size() << std::endl;
    std::cout << "empty() == " << std::boolalpha << c1.empty() << std::endl;
    return (0);
}
[c] [b] [a]
size == 0
empty() == true
[e] [d]
size == 2
empty() == false
 size_type
两个元素间的无符号距离的类型。
typedef T2 size_type;
备注
无符号的整数类型描述可表示任何受控序列长度的对象。 在此处描述为实现定义的 T2 类型的同义词。
示例
// std__unordered_set__unordered_set_size_type.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    Myset::size_type sz = c1.size();
    std::cout << "size == " << sz << std::endl;
    return (0);
}
size == 0
 swap
交换两个容器的内容。
void swap(unordered_set& right);
参数
right
要交换的容器。
备注
成员函数交换 *this 和 right 之间的受控序列。 如果为 unordered_set::get_allocator() == right.get_allocator(),则它在固定时间内执行此操作,它仅在对类型 Tr 的存储的特征对象进行复制时引发异常,并且不使任何引用、指针或指定两个受控序列中的元素的迭代器失效。 否则,它所执行的元素分配和构造函数调用数量会与两个受控序列中的元素数量成正比。
示例
// std__unordered_set__unordered_set_swap.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    Myset c2;
    c2.insert('d');
    c2.insert('e');
    c2.insert('f');
    c1.swap(c2);
    // display contents "[f] [e] [d] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    swap(c1, c2);
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    return (0);
}
[c] [b] [a]
[f] [e] [d]
[c] [b] [a]
 unordered_set
构造容器对象。
unordered_set(const unordered_set& Right);
explicit unordered_set(
    size_typebucket_count = N0,
    const Hash& Hash = Hash(),
    const Comp& Comp = Comp(),
    const Allocator& Al = Alloc());
unordered_set(unordered_set&& Right);
unordered_set(initializer_list<Type> IList);
unordered_set(initializer_list<Type> IList, size_typebucket_count);
unordered_set(
    initializer_list<Type> IList,
    size_typebucket_count,
    const Hash& Hash);
unordered_set(
    initializer_list<Type> IList,
    size_typebucket_count,
    const Hash& Hash,
    const Comp& Comp);
unordered_set(
    initializer_list<Type> IList,
    size_typebucket_count,
    const Hash& Hash,
    const Comp& Comp,
    const Allocator& Al);
template <class InputIterator>
unordered_set(
    InputIteratorfirst,
    InputIteratorlast,
    size_typebucket_count = N0,
    const Hash& Hash = Hash(),
    const Comp& Comp = Comp(),
    const Allocator& Al = Alloc());
参数
InputIterator
迭代器类型。
Al
要存储的分配器对象。
Comp
要存储的比较函数对象。
Hash
要存储的哈希函数对象。
bucket_count
存储桶的最少数量。
Right
要复制的容器。
IList
包含要复制的元素的 initializer_list。
备注
第一个构造函数指定通过 Right 控制的序列副本。 第二个构造函数指定空的受控序列。 第三个构造函数通过移动 Right 指定序列副本。第四个到第八个构造函数使用 initializer_list 指定要复制的元素。 第九个构造函数插入元素值 [first, last) 的序列。
所有构造函数还初始化若干存储的值。 对于复制构造函数,值从 Right 获取。 否则:
存储桶的最少数量是参数 bucket_count(如果有);否则它是此处描述为实现定义值 N0 的默认值。
哈希函数对象是参数 Hash(如果有);否则为 Hash()。
比较函数对象是参数 Comp(如果有);否则为 Comp()。
分配器对象是参数 Al(如果有);否则为 Alloc()。
 value_type
元素的类型。
typedef Key value_type;
备注
该类型描述了受控序列的元素。
示例
// std__unordered_set__unordered_set_value_type.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::unordered_set<char> Myset;
int main()
{
    Myset c1;
    c1.insert('a');
    c1.insert('b');
    c1.insert('c');
    // display contents "[c] [b] [a] "
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    // add a value and reinspect
    Myset::key_type key = 'd';
    Myset::value_type val = key;
    c1.insert(val);
    for (Myset::const_iterator it = c1.begin(); it != c1.end(); ++it)
        std::cout << "[" << *it << "] ";
    std::cout << std::endl;
    return (0);
}
[c] [b] [a]
[d] [c] [b] [a]