命名空间
变体
动作

std::multimap 的推导指引

来自 cppreference.cn
< cpp‎ | container‎ | multimap
 
 
 
 
定义于头文件 <map>
template< class InputIt,

          class Comp = std::less<iter_key_t<InputIt>>,
          class Alloc = std::allocator<iter_to_alloc_t<InputIt>> >
multimap( InputIt, InputIt, Comp = Comp(), Alloc = Alloc() )

    -> multimap<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>> >
multimap( std::initializer_list<std::pair<Key, T>>, Comp = Comp(), Alloc = Alloc() )

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

multimap( InputIt, InputIt, Alloc )
    -> multimap<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 >

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

    -> multimap<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>> >
multimap( std::from_range_t, R&&, Compare = Compare(), Alloc = Alloc() )

    -> multimap<range_key_t<R>, range_mapped_t<R>, Compare, Alloc>;
(5) (自 C++23 起)
template< ranges::input_range R, class Alloc >

multimap( std::from_range_t, R&&, Alloc )

    -> multimap<range_key_t<R>, range_mapped_t<R>, std::less<range_key_t<R>>, Alloc>;
(6) (自 C++23 起)
仅用于演示的辅助类型别名
template< class InputIter >

using iter_val_t =

    typename std::iterator_traits<InputIter>::value_type;
(仅为说明目的而定义*)
template< class InputIter >

using iter_key_t =

    std::remove_const_t< std::tuple_element_t<0, iter_val_t<InputIter>>>;
(仅为说明目的而定义*)
template< class InputIter >

using iter_mapped_t =

    std::tuple_element_t<1, iter_val_t<InputIter>>;
(仅为说明目的而定义*)
template< class InputIter >

using iter_to_alloc_t =
    std::pair<std::add_const_t<tuple_element_t<0, iter_val_t<InputIter>>>,

              std::tuple_element_t<1, iter_val_t<InputIter>>>;
(仅为说明目的而定义*)
template< ranges::input_range Range >

using range_key_t =

    std::remove_const_t<typename ranges::range_value_t<Range>::first_type>;
(自 C++23 起)
(仅为说明目的而定义*)
template< ranges::input_range Range >

using range_mapped_t =

    typename ranges::range_value_t<Range>::second_type;
(自 C++23 起)
(仅为说明目的而定义*)
template< ranges::input_range Range >

using range_to_alloc_t =
    std::pair<std::add_const_t<typename ranges::range_value_t<Range>::first_type>,

              typename ranges::range_value_t<Range>::second_type>;
(自 C++23 起)
(仅为说明目的而定义*)
1-4) 这些 推导指引multimap 而提供,以允许从迭代器范围(重载 (1,3))和 std::initializer_list (重载 (2,4))进行推导。
5,6) 这些推导指引为 multimap 而提供,以允许从 std::from_range_t 标签和 input_range 进行推导。

这些重载仅在 InputIt 满足 遗留输入迭代器 (LegacyInputIterator)Alloc 满足 分配器 (Allocator),且 Comp 不满足 分配器 (Allocator) 时参与重载决议。

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

[编辑] 注解

特性测试 Std 特性
__cpp_lib_containers_ranges 202202L (C++23) 范围感知 构造和插入;重载 (5,6)

[编辑] 示例

#include <map>
 
int main()
{
    // std::multimap m1 = {{"foo", 1}, {"bar", 2}};
        // Error: braced-init-list has no type; cannot deduce
        // pair<Key, T> from {"foo", 1} or {"bar", 2}
 
    std::multimap m1 = {std::pair{"foo", 2}, {"bar", 3}}; // guide #2
    std::multimap m2(m1.begin(), m1.end()); // guide #1
}

[编辑] 缺陷报告

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

DR 应用于 已发布行为 正确行为
LWG 3025 C++17 initializer-list 指引接受 std::pair<const Key, T> 使用 std::pair<Key, T>