名稱空間
變體
操作

std::multimap 的推導指南

來自 cppreference.com
< cpp‎ | 容器‎ | 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 滿足 LegacyInputIteratorAlloc 滿足 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::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++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 3025 C++17 initializer-list 推導指南採用 std::pair<const Key, T> 使用 std::pair<Key, T>