Namespaces
Variants

deduction guides for std::map

From cppreference.net

<td class="t-d
定义于头文件 <map>
template < class InputIt,

class Comp = std:: less < /*iter-key-t*/ < InputIt >> ,
class Alloc = std:: allocator < /*iter-to-alloc-t*/ < InputIt >> >
map ( InputIt, InputIt, Comp = Comp ( ) , Alloc = Alloc ( ) )

- > map < /*iter-key-t*/ < InputIt > , /*iter-val-t*/ < InputIt > , Comp, Alloc > ;
(1) (C++17 起)
template < class Key,

class T,
class Comp = std:: less < Key > ,
class Alloc = std:: allocator < std:: pair < const Key, T >> >
map ( std:: initializer_list < std:: pair < Key, T >> , Comp = Comp ( ) , Alloc = Alloc ( ) )

- > map < Key, T, Comp, Alloc > ;
(2) (C++17 起)
template < class InputIt, class Alloc >

map ( InputIt, InputIt, Alloc )
- > map < /*iter-key-t*/ < InputIt > , /*iter-val-t*/ < InputIt > ,

std:: less < /*iter-key-t*/ < InputIt >> , Alloc > ;
(3) (C++17 起)
template < class Key, class T, class Alloc >

map ( std:: initializer_list < std:: pair < Key, T >> , Alloc )

- > map < Key, T, std:: less < Key > , Alloc > ;
(4) (C++17 起)
template < ranges:: input_range R, class Compare = std:: less < /*range-key-t*/ < R > ,

class Alloc = std:: allocator < /*range-to-alloc-t*/ < R >> >
map ( std:: from_range_t , R && , Compare = Compare ( ) , Alloc = Alloc ( ) )

- > map < /*range-key-t*/ < R > , range_mapped_t < R > , Compare, Alloc > ;
(5) (C++23 起)
template < ranges:: input_range R, class Alloc >

map ( std:: from_range_t , R && , Alloc )
- > map < /*range-key-t*/ < R > , range_mapped_t < R > ,

map std:: less < /*range-key-t*/ < R >> , Alloc > ;
(6) (C++23 起)
仅用于说明的辅助类型别名
template < class InputIt >

using /*iter-val-t*/ =

typename std:: iterator_traits < InputIt > :: value_type ;
( 仅用于说明* )
template < class InputIt >

using /*iter-key-t*/ =

std:: remove_const_t < std:: tuple_element_t < 0 , /*iter-val-t*/ < InputIt >>> ;
( 仅用于说明* )
template < class InputIt >

using /*iter-mapped-t*/ =

std:: tuple_element_t < 1 , /*iter-val-t*/ < InputIt >> ;
1-4) 这些 推导指引 map 提供,以允许从迭代器范围(重载 (1,3) )和 std::initializer_list (重载 (2,4) )进行推导。
5,6) 这些推导指南是为 map 提供的,允许从 std::from_range_t 标签和 input_range 进行推导。

这些重载仅当满足以下条件时参与重载决议: InputIt 满足 LegacyInputIterator 要求, Alloc 满足 Allocator 要求,且 Comp 不满足 Allocator 要求。

注意:库判定某个类型不满足 LegacyInputIterator 要求的程度是未指定的,但至少整型类型不符合输入迭代器的要求。同样地,库判定某个类型不满足 Allocator 要求的程度也是未指定的,但至少要求成员类型 Alloc::value_type 必须存在,且表达式 std:: declval < Alloc & > ( ) . allocate ( std:: size_t { } ) 在被视为未求值操作数时必须保持良构。

说明

功能测试 标准 功能特性
__cpp_lib_containers_ranges 202202L (C++23) 支持范围 的构造与插入;重载版本 ( 5,6 )

示例

#include <map>
int main()
{
    // std::map m1 = {{"foo", 1}, {"bar", 2}};
        // 错误:大括号初始化列表没有类型;无法从 {"foo", 1} 或 {"bar", 2} 推导 pair<Key, T>
    std::map m1 = {std::pair{"foo", 2}, {"bar", 3}}; // 推导指南 #2
    std::map m2(m1.begin(), m1.end()); // 推导指南 #1
}

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

DR 适用范围 发布时的行为 正确行为
LWG 3025 C++17 初始化列表推导指南 ( 2,4 ) 采用 std:: pair < const Key, T > 使用 std:: pair < Key, T >