名稱空間
變體
操作

std::map<Key,T,Compare,Allocator>::map

來自 cppreference.com
< cpp‎ | 容器‎ | map
 
 
 
 
(1)
map();
(C++11 前)
map()
    : map(Compare()) {}
(C++11 起)
explicit map( const Compare& comp,
              const Allocator& alloc = Allocator() );
(2)
explicit map( const Allocator& alloc );
(3) (C++11 起)
template< class InputIt >

map( InputIt first, InputIt last,
     const Compare& comp = Compare(),

     const Allocator& alloc = Allocator() );
(4)
template< class InputIt >

map( InputIt first, InputIt last,
     const Allocator& alloc )

    : map(first, last, Compare(), alloc) {}
(5) (C++14 起)
map( const map& other );
(6)
map( const map& other, const Allocator& alloc );
(7) (C++11 起)
map( map&& other );
(8) (C++11 起)
map( map&& other, const Allocator& alloc );
(9) (C++11 起)
map( std::initializer_list<value_type> init,

     const Compare& comp = Compare(),

     const Allocator& alloc = Allocator() );
(10) (C++11 起)
map( std::initializer_list<value_type> init,

     const Allocator& alloc )

    : map(init, Compare(), alloc) {}
(11) (C++14 起)
template< container-compatible-range<value_type> R >

map( std::from_range_t, R&& rg,
     const Compare& comp = Compare(),

     const Allocator& alloc = Allocator() );
(12) (C++23 起)
template< container-compatible-range<value_type> R >

map( std::from_range_t, R&& rg,
     const Allocator& alloc )

    : map(std::from_range, std::forward<R>(rg), Compare(), alloc) {}
(13) (C++23 起)

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

1-3) 構造一個空容器。
4,5) 構造一個容器,其內容來自範圍 [firstlast)。如果範圍中有多個元素的鍵比較相等,則插入哪個元素是未指定的(待定 LWG2844)。
如果 [firstlast) 不是 有效範圍,則行為未定義。
6,7) 複製建構函式。構造一個容器,其內容是 other 的副本。

如果未提供 alloc,則透過呼叫 std::allocator_traits<allocator_type>::
    select_on_container_copy_construction(other.get_allocator())
獲取分配器。

(C++11 起)

類模板引數推導期間,只有第一個引數有助於推導容器的 Allocator 模板引數。

(C++23 起)
8,9) 移動建構函式。使用移動語義構造一個容器,其內容來自 other。如果未提供 alloc,則透過從 other 的分配器進行移動構造來獲取分配器。

類模板引數推導期間,只有第一個引數有助於推導容器的 Allocator 模板引數。

(C++23 起)
10,11) 初始化列表建構函式。構造一個容器,其內容來自初始化列表 init。如果範圍中有多個元素的鍵比較相等,則插入哪個元素是未指定的(待定 LWG2844)。
12,13) 構造一個容器,其內容來自 rg。如果範圍中有多個元素的鍵比較相等,則插入哪個元素是未指定的(待定 LWG2844)。

目錄

[編輯] 引數

alloc - 用於此容器所有記憶體分配的分配器
comp - 用於所有鍵比較的比較函式物件
first, last - 定義要複製的元素的源 範圍 的迭代器對
其他 - 另一個容器,用作初始化容器元素的源
init - 用於初始化容器元素的初始化列表
rg - 一個容器兼容範圍,即一個input_range,其元素可轉換為value_type
型別要求
-
InputIt 必須滿足 LegacyInputIterator 的要求。
-
Compare 必須滿足 Compare 的要求。
-
Allocator 必須滿足 Allocator 的要求。

[編輯] 複雜度

1-3) 常數。
4,5) N·log(N),其中 N 通常是 std::distance(first, last),如果 [firstlast) 已按 value_comp() 排序,則為 N 的線性。
6,7) 線性於 other 的大小。
8,9) 常數時間。如果給出了 allocalloc != other.get_allocator(),則為線性時間。
10,11) N·log(N),其中 N 通常是 init.size(),如果 init 已按 value_comp() 排序,則為 N 的線性。
12,13) N·log(N),其中 N 通常是 ranges::distance(rg),如果 rg 已按 value_comp() 排序,則為 N 的線性。

[編輯] 異常

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

[編輯] 注意

在容器移動構造(過載 (8,9))之後,指向 other 的引用、指標和迭代器(除了 end 迭代器)仍然有效,但它們引用的是現在在 *this 中的元素。當前標準透過 [container.reqmts]/67 中的籠統宣告來保證這一點,並且正在透過 LWG issue 2321 考慮更直接的保證。

儘管直到 C++23 才正式要求,但一些實現在早期模式中已經將模板引數 Allocator 放入非推導上下文中。

特性測試 標準 特性
__cpp_lib_containers_ranges 202202L (C++23) 範圍感知 構造和插入;過載 (12,13)

[編輯] 示例

#include <iomanip>
#include <iostream>
#include <map>
#include <string>
 
template<typename Key, typename Value, typename Cmp>
std::ostream& operator<<(std::ostream& os, std::map<Key, Value, Cmp> const& map)
{
    os << "{ ";
    for (auto comma{map.size()}; auto const& p : map)
        os << '\'' << p.first << "' is " << p.second << (--comma ? ", " : " ");
    return os << "}\n";
}
 
struct Point
{
    double x, y;
 
    friend std::ostream& operator<<(std::ostream& os, Point pt)
    {
        return os << '(' << pt.x << ", " << pt.y << ')';
    }
};
 
struct PointCmp
{
    bool operator()(const Point& lhs, const Point& rhs) const
    {
        return lhs.x < rhs.x; // NB: y is intentionally ignored
    }
};
 
int main()
{
    // (1) Default constructor
    std::map<std::string, int> map1;
    map1["something"] = 69;
    map1["anything"] = 199;
    map1["that thing"] = 50;
    std::cout << "map1 = " << map1;
 
    // (4) Range constructor
    std::map<std::string, int> iter(map1.find("anything"), map1.end());
    std::cout << "\niter = " << iter;
    std::cout << "map1 = " << map1;
 
    // (6) Copy constructor
    std::map<std::string, int> copied(map1);
    std::cout << "\ncopied = " << copied;
    std::cout << "map1 = " << map1;
 
    // (8) Move constructor
    std::map<std::string, int> moved{std::move(map1)};
    std::cout << "\nmoved = " << moved;
    std::cout << "map1 = " << map1;
 
    // (10) Initializer list constructor
    const std::map<std::string, int> init
    {
        {"this", 100},
        {"can", 100},
        {"be", 100},
        {"const", 100}
    };
    std::cout << "\ninit = " << init;
 
    std::cout << "\nCustom Key class option 1:\n";
    // Use a comparison struct
    std::map<Point, double, PointCmp> mag =
    {
        {{5, -12}, 13},
        {{3, 4}, 5},
        {{-8, -15}, 17}
    };
    std::cout << "mag = " << mag << '\n';
 
    std::cout << "Custom Key class option 2:\n";
    // Use a comparison lambda
    // This lambda sorts points according to their magnitudes, where
    // these magnitudes are taken from the local variable mag.
    auto cmpLambda = [&mag](const Point& lhs, const Point& rhs)
    {
        return mag[lhs] < mag[rhs];
    };
 
    // You could also use a lambda that is not dependent on local variables, like this:
    // auto cmpLambda = [](const Point& lhs, const Point& rhs){ return lhs.y < rhs.y; };
    std::map<Point, double, decltype(cmpLambda)> magy(cmpLambda);
 
    // Various ways of inserting elements:
    magy.insert(std::pair<Point, double>({5, -12}, 13));
    magy.insert({{3, 4}, 5});
    magy.insert({Point{-8.0, -15.0}, 17});    
    std::cout << "magy = " << magy << '\n';
 
    std::cout << "Construction from a range:\n";
    using PS = std::pair<const std::string, int>;
    const auto rg = {PS{"one", 1}, {"one", 101}, {"two", 2}, {"three", 3}};
#if __cpp_lib_containers_ranges
    std::map<std::string, int> nums(std::from_range, rg); // overload (12)
#else
    std::map<std::string, int> nums(rg.begin(), rg.end()); // fallback to (4)
#endif
    std::cout << "nums = " << nums << '\n';
}

輸出

map1 = { 'anything' is 199, 'something' is 69, 'that thing' is 50 }
 
iter = { 'anything' is 199, 'something' is 69, 'that thing' is 50 }
map1 = { 'anything' is 199, 'something' is 69, 'that thing' is 50 }
 
copied = { 'anything' is 199, 'something' is 69, 'that thing' is 50 }
map1 = { 'anything' is 199, 'something' is 69, 'that thing' is 50 }
 
moved = { 'anything' is 199, 'something' is 69, 'that thing' is 50 }
map1 = { }
 
init = { 'be' is 100, 'can' is 100, 'const' is 100, 'this' is 100 }
 
Custom Key class option 1:
mag = { '(-8, -15)' is 17, '(3, 4)' is 5, '(5, -12)' is 13 }
 
Custom Key class option 2:
magy = { '(3, 4)' is 5, '(5, -12)' is 13, '(-8, -15)' is 17 }
 
Construction from a range:
nums = { 'one' is 1, 'three' is 3, 'two' is 2 }

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 2076 C++11 過載 (4) 有條件地要求 KeyT 可以 CopyInsertable*this 未要求
LWG 2193 C++11 預設建構函式是 explicit 的 已改為非 explicit

[編輯] 另請參閱

將值賦給容器
(public member function) [編輯]