命名空間
變體
動作

std::unordered_set<Key,Hash,KeyEqual,Allocator>::unordered_set

出自 cppreference.com
 
 
 
 
(1)
unordered_set()
    : unordered_set(size_type(/* 未指定 */)) {}
(C++11 起)
(直到 C++20)
unordered_set();
(自 C++20 起)
explicit unordered_set( size_type bucket_count,

                        const Hash& hash = Hash(),
                        const key_equal& equal = key_equal(),

                        const Allocator& alloc = Allocator() );
(2) (C++11 起)
unordered_set( size_type bucket_count,

               const Allocator& alloc )

    : unordered_set(bucket_count, Hash(), key_equal(), alloc) {}
(3) (C++14 起)
unordered_set( size_type bucket_count,

               const Hash& hash,
               const Allocator& alloc )

    : unordered_set(bucket_count, hash, key_equal(), alloc) {}
(4) (C++14 起)
explicit unordered_set( const Allocator& alloc );
(5) (C++11 起)
template< class InputIt >

unordered_set( InputIt first, InputIt last,
               size_type bucket_count = /* 未指定 */,
               const Hash& hash = Hash(),
               const key_equal& equal = key_equal(),

               const Allocator& alloc = Allocator() );
(6) (C++11 起)
template< class InputIt >

unordered_set( InputIt first, InputIt last,
               size_type bucket_count,
               const Allocator& alloc )
    : unordered_set(first, last,

                    bucket_count, Hash(), key_equal(), alloc) {}
(7) (C++14 起)
template< class InputIt >

unordered_set( InputIt first, InputIt last,
               size_type bucket_count,
               const Hash& hash,
               const Allocator& alloc )
    : unordered_set(first, last,

                    bucket_count, hash, key_equal(), alloc) {}
(8) (C++14 起)
unordered_set( const unordered_set& other );
(9) (C++11 起)
unordered_set( const unordered_set& other, const Allocator& alloc );
(10) (C++11 起)
unordered_set( unordered_set&& other );
(11) (C++11 起)
unordered_set( unordered_set&& other, const Allocator& alloc );
(12) (C++11 起)
unordered_set( std::initializer_list<value_type> init,

               size_type bucket_count = /* 未指定 */,
               const Hash& hash = Hash(),
               const key_equal& equal = key_equal(),

               const Allocator& alloc = Allocator() );
(13) (C++11 起)
unordered_set( std::initializer_list<value_type> init,

               size_type bucket_count,
               const Allocator& alloc )
    : unordered_set(init, bucket_count,

                    Hash(), key_equal(), alloc) {}
(14) (C++14 起)
unordered_set( std::initializer_list<value_type> init,

               size_type bucket_count,
               const Hash& hash,
               const Allocator& alloc )
    : unordered_set(init, bucket_count,

                    hash, key_equal(), alloc) {}
(15) (C++14 起)
template< container-compatible-range<value_type> R >

unordered_set( std::from_range_t, R&& rg,
               size_type bucket_count = /* 見說明 */,
               const Hash& hash = Hash(),
               const key_equal& equal = key_equal(),

               const Allocator& alloc = Allocator() );
(16) (自 C++23 起)
template< container-compatible-range<value_type> R >

unordered_set( std::from_range_t, R&& rg,
               size_type bucket_count,
               const Allocator& alloc )
    : unordered_set(std::from_range, std::forward<R>(rg),

                    bucket_count, Hash(), key_equal(), alloc) {}
(17) (自 C++23 起)
template< container-compatible-range<value_type> R >

unordered_set( std::from_range_t, R&& rg,
               size_type bucket_count,
               const Hash& hash,
               const Alloc& alloc )
    : unordered_set(std::from_range, std::forward<R>(rg),

                    bucket_count, hash, key_equal(), alloc) {}
(18) (自 C++23 起)

從各種資料來源建構新容器。可選擇性使用使用者提供的 bucket_count 作為建立的最小桶數,hash 作為雜湊函數,equal 作為鍵值比較函數,以及 alloc 作為分配器。

1-5) 建構空容器。將 max_load_factor() 設為 1.0。對於預設建構函式,桶的數量未指定。
6-8) 使用範圍 [firstlast) 中的內容建構容器。將 max_load_factor() 設為 1.0。若範圍內有多個元素的鍵值比較相等,則插入哪一個元素是不確定的(待處理 LWG2844)。
9,10) 複製建構函式。使用 other 的內容副本建構容器,同時複製負載因子、謂詞和雜湊函數。若未提供 alloc,則分配器透過呼叫 std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) 取得。

樣板參數 Allocator 僅在用於 類別樣板參數推導 時才會從第一個引數進行推導。

(自 C++23 起)
11,12) 移動建構函式。使用移動語意以 other 的內容建構容器。若未提供 alloc,則分配器透過從屬於 other 的分配器進行移動建構取得。

樣板參數 Allocator 僅在用於 類別樣板參數推導 時才會從第一個引數進行推導。

(自 C++23 起)
13-15) 初始化列表建構函式。使用初始化列表 init 的內容建構容器,與 unordered_set(init.begin(), init.end()) 相同。
16-18) 使用 rg 的內容建構容器。若範圍內有多個元素的鍵值比較相等,則插入哪一個元素是不確定的(待處理 LWG2844)。

目錄

[edit] 參數

alloc - 用於此容器所有記憶體配置的配置器
bucket_count - 初始化時使用的最小桶數。若未指定,將使用一個未指定的預設值
hash - 要使用的雜湊函數
equal - 此容器進行所有鍵值比較時所使用的比較函數
first, last - 定義要複製的元素之來源範圍的迭代器對
rg - 一個 容器相容範圍,即一個其元素可轉換為 value_typeinput_range
其他 - 另一個用作來源以初始化容器元素的容器
init - 用於初始化容器元素的初始化列表
類型要求
-
InputIt 必須符合 傳統輸入迭代器 (LegacyInputIterator) 的要求。

[edit] 複雜度

1-5) 常數。
6-8) 平均情況為線性(即 O(N),其中 Nstd::distance(first, last)),最差情況為二次方,即 O(N2)
9,10)other 的大小呈線性關係。
11,12) 常數。若提供了 allocalloc != other.get_allocator(),則為線性。
13-15) 平均情況為 O(N)Nstd::size(init)),最差情況為 O(N2)
16-18) 平均情況為 O(N)Nranges::distance(rg)),最差情況為 O(N2)

[edit] 例外

Allocator::allocate 的呼叫可能拋出例外。

[edit] 備註

容器移動建構(多載 (4))之後,指向 other 的參考、指標與迭代器(結束迭代器除外)仍然有效,但會指向目前位於 *this 中的元素。現行標準透過 [container.reqmts]/67 中的廣泛陳述提供此項保證,更直接的保證正透過 LWG issue 2321 研議中。

儘管在 C++23 之前並未正式要求,但一些實作在早期模式中就已將樣板參數 Allocator 置入 非推導情境 中。

功能測試巨集 數值 標準 功能
__cpp_lib_containers_ranges 202202L (C++23) 範圍感知建構與插入;多載 (16-18)

[edit] 範例

[edit] 缺陷報告

下列更改行為的缺陷報告追溯應用於之前的 C++ 標準。

DR 應用於 出版時的行為 正確的行為
LWG 2193 C++11 預設建構函式 (1) 為 explicit 改為非 explicit
LWG 2230 C++11 多載 (13) 的語意未指定 已指定

[edit] 參見

指派值給容器
(公開成員函式) [編輯]
English Deutsch 日本語 中文(简体) 中文(繁體)