Share via


unordered_set 類別

類別範本描述一個物件,該物件會控制 型 const Key 別 元素的不同長度序列。 序列由雜湊函式弱式排序,將序列分割為子序列的已排序集合,稱為 Bucket。 在每個貯體內,比較函式會決定任何一組元素是否具有對等的順序。 每個項目同時做為排序鍵和值。 序列表示允許以一些作業查閱、插入和移除任意項目,這些作業可以獨立於序列中的項目數目 (常數時間),至少當所有 Bucket 長度大約相等時。 在最壞的情況下,當所有項目都在一個 Bucket 時,作業數目與序列中的項目數目成正比 (線性時間)。 插入專案不會使反覆運算器失效,而移除元素只會使指向已移除元素的反覆運算器失效。

語法

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
配置器類別。

成員

Typedefs

名稱 描述
allocator_type 管理儲存體的配置器類型。
const_iterator 用於受控制序列的常數迭代器類型。
const_local_iterator 用於受控制序列的常數 Bucket 迭代器類型。
const_pointer 項目的常數指標類型。
const_reference 項目的常數參考類型。
difference_type 兩個項目之間帶正負號距離的類型。
hasher 雜湊函式的類型。
iterator 受控制序列之迭代器的類型。
key_equal 比較函式的類型。
key_type 排序索引鍵的類型。
local_iterator 用於受控制序列的 Bucket 迭代器類型。
pointer 項目的指標類型。
reference 項目的參考類型。
size_type 兩個項目之間不帶正負號距離的類型。
value_type 元素的類型。

函式

名稱 描述
begin 指定受控制序列的開頭。
bucket 取得索引鍵值的值區數目。
bucket_count 取得 Bucket 的數目。
bucket_size 取得 Bucket 大小。
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 取得 Bucket 最大數目。
max_load_factor 取得或設定每個 Bucket 最大項目數。
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 類型的所有值 YKey,呼叫 key_eq()(X, Y) 才會傳回 true,呼叫 hash_function()(keyval) 會產生 size_t 類型值的分佈。 不同于類別範本 unordered_multiset 類別 ,類型的 unordered_set 物件可確保 key_eq()(X, Y) 受控制序列的任何兩個專案一律為 false。 (索引鍵是唯一的)。

物件也會儲存最大載入因數,指定每個 Bucket 所需的項目平均數目上限。 如果插入專案會導致 unordered_set::load_factor() 超過最大負載因數,容器會增加貯體數目,並視需要重建雜湊表。

受控制序列中實際的項目順序取決於雜湊函式、比較函式、插入順序、最大載入因數和 Bucket 目前數目。 您通常無法預測受控制序列中的元素順序。 不過,您永遠可以確保,有對等順序的任何項目子集在受控制序列中為相鄰。

物件會透過 類型的 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

取得 Bucket 的數目。

size_type bucket_count() const;

備註

成員函式會傳回目前的 Bucket 數目。

範例

// 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

取得 Bucket 大小

size_type bucket_size(size_type nbucket) const;

參數

nbucket
值區數目。

備註

成員函式會傳回 Bucket 編號 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 視為任何支援 begin()cbegin() 且可修改 (非 const) 的容器類型。

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 視為任何支援 end()cend() 且可修改 (非 const) 的容器類型。

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::eraseunordered_set::beginunordered_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

用於受控制序列的常數 Bucket 迭代器類型。

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 /std:c++latest
#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 的專案,除非它已經包含其值相等排序的專案。

傳回值

如果已插入,pairbool 元件就會傳回 true,如果 unordered_set 已經包含元素,其中該元素的索引鍵具有對等的排序值,且其迭代器元件傳回插入新元件或該元素已在的位址,則會傳回 false。

若要存取此成員函式所傳回的配對 pr 迭代器元件,請使用 pr.first,若要取其值,請使用 *(pr.first)。 若要存取此成員函式所傳回之配對 prbool 元件,請使用 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
樣板參數,指定 可用來建構 的 元素的 value_type 引數類型 unordered_set ,並以完美轉送 Val 做為引數。

First
要複製之第一個元素的位置。

Last
要複製之最一個元素後方的位置。

InputIterator
符合輸入反覆運算器 需求的 樣板函式引數,指向可用來建構 value_type 物件之型別的專案。

IList
initializer_list要從中複製專案的 。

傳回值

單一元素成員函式 (1) 和 (2) 會傳回 ,如果已建立插入,則傳回其 bool 元件為 true 的 ,如果 unordered_set 已經包含索引鍵具有排序中相等值的專案,則傳回 pair false。 如果 bool 元件為 ,則傳回值組的反覆運算器元件會指向新插入的專案,如果 bool 元件為 truefalse ,則指向現有專案。

單一元素與提示成員函式 (3) 和 (4), 會傳回反覆運算器,指向插入新元素 unordered_set 的位置,如果具有對等索引鍵的專案已經存在,則傳回反覆運算器。

備註

此函式不會使任何迭代器、指標或參考無效。

在只插入一個專案期間,如果擲回例外狀況,但不會發生在容器的雜湊函式中,則不會修改容器的狀態。 若雜湊函式中擲回例外狀況,則結果為未定義。 在插入多個元素期間,若擲出例外狀況,則容器會處於未指定但有效的狀態。

若要存取單一元素成員函式所傳回 之 pairpr 的反覆運算器元件,請使用 ;若要取值傳回配對內的反覆運算器,請使用 pr.first*pr.first ,並提供元素。 若要存取 bool 元件,請使用 pr.second。 例如,請參閱本文中稍後的範例程式碼。

value_type容器的 是屬於容器的 typedef,而 set 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::emplaceset::emplace_hint

如需程式碼範例,請參閱set::insert

iterator

類型,提供常數 正向反覆運算器 ,可讀取unordered_set中的專案。

typedef implementation-defined iterator;

範例

如需如何宣告和使用 反覆運算器的 範例 begin ,請參閱範例。

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

取得 Bucket 最大數目。

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

取得或設定每個 Bucket 最大項目數。

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
要交換的容器。

備註

成員函式會交換 *thisright 之間受控制的序列。 如果 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
Bucket 最小數目。

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]