Namespaces
Variants

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

From cppreference.net

(1)
map ( ) ;
(C++11 前)
map ( ) : map ( Compare ( ) ) { }
(C++11 起)
(C++26 起为 constexpr)
explicit map ( const Compare & comp,
const Allocator & alloc = Allocator ( ) ) ;
(2) (C++26 起为 constexpr)
explicit map ( const Allocator & alloc ) ;
(3) (C++11 起)
(C++26 起为 constexpr)
template < class InputIt >

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

const Allocator & alloc = Allocator ( ) ) ;
(4) (C++26 起为 constexpr)
template < class InputIt >

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

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

const Compare & comp = Compare ( ) ,

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

const Allocator & alloc )

: map ( init, Compare ( ) , alloc ) { }
(11) (C++14 起)
(C++26 起为 constexpr)
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 起)
(C++26 起为 constexpr)
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 起)
(C++26 起为 constexpr)

从多种数据源构造新容器,并可选择使用用户提供的分配器 alloc 或比较函数对象 comp

1-3) 构造一个空容器。
4,5) 使用范围 [ first , last ) 的内容构造容器。
如果 [ first , last ) 不是有效的 范围 ,则行为未定义。
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 模板参数的推导。

(since C++23)
10,11) 使用初始化列表 init 的内容构造容器。
12,13) 使用 rg 的内容构造容器。

目录

参数

alloc - 用于此容器所有内存分配的分配器
comp - 用于所有键值比较的比较函数对象
first, last - 定义待复制元素源 范围 的迭代器对
other - 用作源容器以初始化当前容器元素的其他容器
init - 用于初始化容器元素的初始化列表
rg - 容器兼容范围 ,即元素可转换为 value_type input_range
类型要求
-
InputIt 必须满足 LegacyInputIterator 要求
-
Compare 必须满足 Compare 要求
-
Allocator 必须满足 Allocator 要求

复杂度

1-3) 常量。
4,5) N·log(N) 其中 N std:: distance ( first, last ) 的返回值。通常情况下时间复杂度为 N·log(N),若 [ first , last ) 范围已按 value_comp ( ) 排序,则时间复杂度为线性。
6,7) other 的大小成线性关系。
8,9) 常数复杂度。若提供 alloc alloc ! = 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 的引用、指针和迭代器(除尾后迭代器外)保持有效,但会引用现在位于 * this 中的元素。当前标准通过 [container.reqmts]/67 中的总体说明提供此保证,更直接的保证正在通过 LWG issue 2321 进行审议。

如果范围内多个元素的键比较等价,则未指定插入哪个元素(待解决 LWG2844 )。

尽管在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, const std::map<Key, Value, Cmp>& map)
{
    os << "{ ";
    for (auto comma{map.size()}; const auto& 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; // 注意:y 坐标被有意忽略
    }
};
int main()
{
    // (1) 默认构造函数
    std::map<std::string, int> map1;
    map1["something"] = 69;
    map1["anything"] = 199;
    map1["that thing"] = 50;
    std::cout << "map1 = " << map1;
    // (4) 范围构造函数
    std::map<std::string, int> iter(map1.find("anything"), map1.end());
    std::cout << "\niter = " << iter;
    std::cout << "map1 = " << map1;
    // (6) 拷贝构造函数
    std::map<std::string, int> copied(map1);
    std::cout << "\ncopied = " << copied;
    std::cout << "map1 = " << map1;
    // (8) 移动构造函数
    std::map<std::string, int> moved{std::move(map1)};
    std::cout << "\nmoved = " << moved;
    std::cout << "map1 = " << map1;
    // (10) 初始化列表构造函数
    const std::map<std::string, int> init
    {
        {"this", 100},
        {"can", 100},
        {"be", 100},
        {"const", 100}
    };
    std::cout << "\ninit = " << init;
    std::cout << "\n自定义键类选项 1:\n";
    // 使用比较结构体
    std::map<Point, double, PointCmp> mag =
    {
        {{5, -12}, 13},
        {{3, 4}, 5},
        {{-8, -15}, 17}
    };
    std::cout << "mag = " << mag << '\n';
    std::cout</

缺陷报告

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

缺陷报告 应用于 发布时行为 正确行为
LWG 2076 C++11 重载 ( 4 ) 有条件地要求 Key T 可复制插入到 * this 无需此要求
LWG 2193 C++11 默认构造函数为显式 改为非显式

另请参阅

为容器赋值
(公开成员函数)