Namespaces
Variants

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

From cppreference.net

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 ( )

从多种数据源构造新的容器适配器,并可选择使用用户提供的比较函数对象 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) 使用范围 [ first , last ) 的内容构造容器适配器,等价于 insert ( first, last ) ;
14,15) (13) 相同。 请参阅下方的 分配器使用说明
16) 使用范围 rg 的内容构造容器适配器。首先使用 (10) 作为 委托构造函数 ,然后通过 insert_range ( std:: forward < R > ( rg ) ) ; 的方式初始化 c
17) (16) 相同,但将其用作 委托构造函数
18,19) (16) 。 参见下方 分配器使用说明
20) 使用区间 [ first , last ) 的内容构造底层容器,如同通过 insert ( first, last ) 实现。
21,22) (20) 相同。 请参阅下方的 分配器使用说明
23) 一个 初始化列表构造函数 。使用初始化列表 init 的内容构造底层容器,通过 (13) 作为 委托构造函数
24,25) (23) 。 参见下方 分配器使用说明
26) 一个 初始化列表构造函数 。使用初始化列表 init 的内容构造底层容器,通过 (20) 作为 委托构造函数
27,28) 保存为 (26) 。 参见下方 分配器使用说明

注意重载版本 (13-15,20-22) :若 [ first , last ) 不是有效的 范围 ,则行为未定义。

关于重载 (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.keys c.values 是通过 使用分配器构造 构建的。 仅当 std:: uses_allocator_v < container_type, Allocator > true 时,这些重载才会参与重载决议。

参数

key_cont - 用作初始化底层键容器来源的容器
mapped_cont - 用作初始化底层值容器来源的容器
other - 用作初始化底层容器元素来源的另一个 flat_map
alloc - 用于底层容器所有内存分配的分配器
comp - 用于所有键比较的函数对象
first, last - 定义要复制的元素来源 范围 的迭代器对
init - 用于初始化底层容器元素的初始化列表
rg - 用作初始化底层容器来源的 容器兼容范围 (即元素可转换为 value_type input_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) 如果输入范围 [ first , last ) 相对于 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) [ first , last ) 的大小成线性。
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 考虑更直接的保证。

示例

<span class="edits