名稱空間
變體
操作

std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>::flat_map

來自 cppreference.com
< cpp‎ | 容器‎ | flat map
 
 
 
 
flat_map()
    : flat_map(key_compare()) { }
(1) (C++23 起)
template< class Allocator >
flat_map( const flat_map&, const Allocator& alloc );
(2) (C++23 起)
template< class Allocator >
flat_map( flat_map&&, const Allocator& alloc );
(3) (C++23 起)
flat_map( key_container_type key_cont, mapped_container_type mapped_cont,
          const key_compare& comp = key_compare() );
(4) (C++23 起)
template< class Allocator >

flat_map( const key_container_type& key_cont,
          const mapped_container_type& mapped_cont,

          const Allocator& alloc );
(5) (C++23 起)
template< class Allocator >

flat_map( const key_container_type& key_cont,
          const mapped_container_type& mapped_cont,

          const key_compare& comp, const Allocator& alloc );
(6) (C++23 起)
flat_map( std::sorted_unique_t, key_container_type key_cont,

          mapped_container_type mapped_cont,

          const key_compare& comp = key_compare() );
(7) (C++23 起)
template< class Allocator >

flat_map( std::sorted_unique_t, const key_container_type& key_cont,

          const mapped_container_type& mapped_cont, const Allocator& alloc );
(8) (C++23 起)
template< class Allocator >

flat_map( std::sorted_unique_t, const key_container_type& key_cont,
          const mapped_container_type& mapped_cont,

          const key_compare& comp, const Allocator& alloc );
(9) (C++23 起)
explicit flat_map( const key_compare& comp )
    : c(), compare(comp) { }
(10) (C++23 起)
template< class Allocator >
flat_map( const key_compare& comp, const Allocator& alloc );
(11) (C++23 起)
template< class Allocator >
explicit flat_map( const Allocator& alloc );
(12) (C++23 起)
template< class InputIter >

flat_map( InputIter first, InputIter last,
          const key_compare& comp = key_compare() )

    : c(), compare(comp);
(13) (C++23 起)
template< class InputIter, class Allocator >

flat_map( InputIter first, InputIter last,

          const key_compare& comp, const Allocator& alloc );
(14) (C++23 起)
template< class InputIter, class Allocator >
flat_map( InputIter first, InputIter last, const Allocator& alloc );
(15) (C++23 起)
template< container-compatible-range<value_type> R >

flat_map( std::from_range_t, R&& rg, const key_compare& comp )

    : flat_map(comp);
(16) (C++23 起)
template< container-compatible-range<value_type> R >

flat_map( std::from_range_t fr, R&& rg )

    : flat_map(fr, std::forward<R>(rg), key_compare()) { }
(17) (C++23 起)
template< container-compatible-range<value_type> R, class Allocator >
flat_map( std::from_range_t, R&& rg, const Allocator& alloc );
(18) (C++23 起)
template< container-compatible-range<value_type> R, class Allocator >

flat_map( std::from_range_t, R&& rg, const key_compare& comp,

          const Allocator& alloc );
(19) (C++23 起)
template< class InputIter >

flat_map( std::sorted_unique_t s, InputIter first, InputIter last,
          const key_compare& comp = key_compare() )

    : c(), compare(comp);
(20) (C++23 起)
template< class InputIter, class Allocator >

flat_map( std::sorted_unique_t s, InputIter first, InputIter last,

          const key_compare& comp, const Allocator& alloc );
(21) (C++23 起)
template< class InputIter, class Allocator >

flat_map( std::sorted_unique_t s, InputIter first, InputIter last,

          const Allocator& alloc );
(22) (C++23 起)
flat_map( std::initializer_list<value_type> init,

          const key_compare& comp = key_compare() )

    : flat_map(init.begin(), init.end(), comp) { }
(23) (C++23 起)
template< class Allocator >

flat_map( std::initializer_list<value_type> init, const key_compare& comp,

          const Allocator& alloc );
(24) (C++23 起)
template< class Allocator >
flat_map( std::initializer_list<value_type> init, const Allocator& alloc );
(25) (C++23 起)
flat_map( std::sorted_unique_t s, std::initializer_list<value_type> init,

          const key_compare& comp = key_compare() )

    : flat_map(s, init.begin(), init.end(), comp) { }
(26) (C++23 起)
template< class Allocator >

flat_map( std::sorted_unique_t s, std::initializer_list<value_type> init,

          const key_compare& comp, const Allocator& alloc );
(27) (C++23 起)
template< class Allocator >

flat_map( std::sorted_unique_t s, std::initializer_list<value_type> init,

          const Allocator& alloc );
(28) (C++23 起)

構造新的容器介面卡,可使用各種資料來源,並可選擇使用使用者提供的比較函式物件 comp 和/或分配器 alloc

1) 預設建構函式。構造一個空的容器介面卡。
2) 複製建構函式。使用 other.c 的內容副本構造 c,並使用 other.compare 構造 compare。參見下文的分配器使用說明
3) 移動建構函式。使用移動語義構造具有 other 內容的容器介面卡。參見下文的分配器使用說明
4) 首先,使用 std::move(key_cont) 初始化 c.keys,使用 std::move(mapped_cont) 初始化 c.values,並使用 comp 初始化 compare。然後,根據 value_comp() 對底層範圍 [begin()end()) 進行排序。最後,刪除重複元素,如同執行:
auto zv = views::zip(c.keys, c.values);
auto it = ranges::unique(zv, key_equiv(compare)).begin();
auto dist = distance(zv.begin(), it);
c.keys.erase(c.keys.begin() + dist, c.keys.end());
c.values.erase(c.values.begin() + dist, c.values.end());
.
5)(4) 相同,等價於 flat_map(key_cont, mapped_cont);。參見下文的分配器使用說明
6)(4) 相同,等價於 flat_map(key_cont, mapped_cont, comp);。參見下文的分配器使用說明
7) 使用 std::move(key_cont) 初始化 c.keys,使用 std::move(mapped_cont) 初始化 c.values,並使用 comp 初始化 compare
8)(7) 相同,等價於 flat_map(s, key_cont, mapped_cont);。參見下文的分配器使用說明
9)(7) 相同,等價於 flat_map(s, key_cont, mapped_cont, comp);。參見下文的分配器使用說明
10) 構造一個空的容器介面卡。
11,12) 構造一個空的容器介面卡。參見下方的分配器使用說明
13) 使用範圍 [firstlast) 的內容構造容器介面卡,等價於 insert(first, last);
14,15)(13)。參見下方的分配器使用說明
16) 使用範圍 rg 的內容構造容器介面卡。首先,使用 (10) 作為委託建構函式。然後,使用 rg 的內容初始化 c,如同執行 insert_range(std::forward<R>(rg));
17)(16),使用其作為委託建構函式
18,19)(16)。參見下方的分配器使用說明
20) 使用範圍 [firstlast) 的內容構造底層容器,如同執行 insert(first, last)
21,22)(20)。參見下方的分配器使用說明
23) 初始化列表建構函式。使用初始化列表 init 的內容構造底層容器,使用 (13) 作為委託建構函式
24,25)(23)。參見下方的分配器使用說明
26) 初始化列表建構函式。使用初始化列表 init 的內容構造底層容器,使用 (20) 作為委託建構函式
27,28)(26)。參見下方的分配器使用說明

對於過載 (13-15,20-22) 的注意事項:如果 [firstlast) 不是有效範圍,則行為未定義。

對於過載 (4-6,13-19,23-25) 的注意事項:如果範圍中的多個元素的鍵比較相等,則插入哪個元素是未指定的(待解決 LWG2844)。

目錄

[編輯] 分配器使用說明

建構函式 (2,3,5,6,8,9,11,12,14,15,17,19,21,22,24,25,27,28) 等效於相應的非分配器建構函式,不同之處在於底層容器 c.keysc.values 使用使用分配器構造。這些過載僅當 std::uses_allocator_v<container_type, Allocator>true 時才參與過載決議。

[編輯] 引數

key_cont - 用作初始化底層鍵容器的源容器
mapped_cont - 用作初始化底層值容器的源容器
其他 - 另一個 flat_map,用作初始化底層容器元素的源
alloc - 用於底層容器所有記憶體分配的分配器
comp - 一個用於所有鍵比較的函式物件
first, last - 定義要複製的元素的源 範圍 的迭代器對
init - 初始化列表,用於初始化底層容器的元素
rg - 容器兼容範圍(即,其元素可轉換為 value_typeinput_range),用作初始化底層容器的源
fr - 一個消歧標籤,指示包含的成員應進行範圍構造
s - 消歧標籤,指示輸入序列相對於 value_comp() 已排序且所有元素唯一
型別要求
-
InputIt 必須滿足 LegacyInputIterator 的要求。
-
Compare 必須滿足 Compare 的要求。
-
Allocator 必須滿足 Allocator 的要求。

[編輯] 複雜度

1) 常數。
2)other 的大小呈線性關係。
3) 與包裝容器的相應移動建構函式相同,即與 cont 的大小呈常數或線性關係。
4-6) 如果 cont 相對於 value_comp() 已排序,則複雜度為 N 的線性時間,否則為 𝓞(N·log(N)),其中 N 是此呼叫前 key_cont.size() 的值。
7-9) 與包裝容器的相應移動建構函式相同,即與 cont 的大小呈常數或線性關係。
10-12) 常數。
13-15) 如果輸入範圍 [firstlast) 相對於 value_comp() 已排序,則複雜度為 N 的線性時間,否則為 𝓞(N·log(N)),其中 N 是此呼叫前 key_cont.size() 的值。
16-19) 如果輸入範圍 rg 相對於 value_comp() 已排序,則複雜度為 N 的線性時間,否則為 𝓞(N·log(N)),其中 N 是此呼叫前 key_cont.size() 的值。
20-22) 複雜度與 [firstlast) 的大小呈線性關係。
23-25) 如果 init 的元素相對於 value_comp() 已排序,則複雜度為 N 的線性時間,否則為 𝓞(N·log(N)),其中 N 是此呼叫前 key_cont.size() 的值。
26-28)init 的大小呈線性關係。

[編輯] 異常

呼叫 Allocator::allocate 可能會丟擲異常。

[編輯] 注意

在容器移動構造(過載 (3))之後,指向 other 的引用、指標和迭代器(除了尾迭代器)仍然有效,但現在指向 *this 中的元素。當前的標準透過 [container.reqmts]/67 中的一攬子宣告提供此保證,並且透過 LWG issue 2321 正在考慮更直接的保證。

[編輯] 示例

[編輯] 參閱

向容器介面卡賦值
(public member function) [編輯]