c++ 11标准模板(STL) std::map(二)

定义于头文件<map>

template<

    class Key,
    class T,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<std::pair<const Key, T> >

> class map;
(1)
namespace pmr {

    template <class Key, class T, class Compare = std::less<Key>>
    using map = std::map<Key, T, Compare,
                         std::pmr::polymorphic_allocator<std::pair<const Key,T>>>

}
(2)(C++17 起)

std::map有序键值对容器,它的元素的键是唯一的。用比较函数 Compare 排序键。搜索、移除和插入操作拥有对数复杂度。 map 通常实现为红黑树。

在每个标准库使用比较 (Compare) 概念的位置,以等价关系检验唯一性。不精确而言,若二个对象 ab 互相比较不小于对方 : !comp(a, b) && !comp(b, a) ,则认为它们等价(非唯一)。

std::map 满足容器 (Container) 、具分配器容器 (AllocatorAwareContainer) 、关联容器 (AssociativeContainer) 和可逆容器 (ReversibleContainer) 的要求。

成员函数

构造 map

std::map<Key,T,Compare,Allocator>::map
map();

explicit map( const Compare& comp,

              const Allocator& alloc = Allocator() );
(1)

explicit map( const Allocator& alloc );

(1)(C++11 起)
template< class InputIt >

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

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

map( InputIt first, InputIt last,

     const Allocator& alloc );
(C++14 起)

map( const map& other );

(3)

map( const map& other, const Allocator& alloc );

(3)(C++11 起)

map( map&& other );

(4)(C++11 起)

map( map&& other, const Allocator& alloc );

(4)(C++11 起)
map( std::initializer_list<value_type> init,

     const Compare& comp = Compare(),

     const Allocator& alloc = Allocator() );
(5)(C++11 起)

map( std::initializer_list<value_type> init,
     const Allocator& );

(C++14 起)

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

1) 构造空容器。

2) 构造容器,使之拥有范围 [first, last) 的内容。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的(待决的 LWG2844 )。

3) 复制构造函数。构造容器,使之拥有 other 的内容副本。若不提供 alloc ,则通过调用 std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) 获得分配器。

4) 移动构造函数。用移动语义构造容器,使之拥有 other 的内容。若不提供 alloc ,则从属于 other 的分配器移动构造分配器

5) 构造容器,使之拥有 initializer_list init 的内容。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的(待决的 LWG2844 )。

参数

alloc-用于此容器所有内存分配的分配器
comp-用于所有关键比较的比较函数对象
first, last-复制元素来源的范围
other-要用作源以初始化容器元素的另一容器
init-用以初始化容器元素的 initializer_list
类型要求
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
- Compare 必须满足比较 (Compare) 的要求。
- Allocator 必须满足分配器 (Allocator) 的要求。

复杂度

1) 常数。

2) N log(N) ,其中通常有 N = std::distance(first, last) ,若范围已为 value_comp() 所排序则与 N 成线性。

3) 与 other 的大小成线性。

4) 常数。若给定 alloc 且 alloc != other.get_allocator() 则为线性。

5) N log(N) ,其中通常有 N = init.size()) ,若 init 已按照 value_comp() 排序则与 N 成线性 。

异常

Allocator::allocate 的调用可能抛出。

析构 map

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

~map();

销毁容器。调用元素的析构函数,然后解分配所用的存储。注意,若元素是指针,则不销毁所指向的对象。

复杂度

与容器大小成线性。

调用示例

#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <map>
#include <time.h>

using namespace std;

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}

    Cell &operator +=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator +(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator *(const Cell &cell)
    {
        x *= cell.x;
        y *= cell.y;
        return *this;
    }

    Cell &operator ++()
    {
        x += 1;
        y += 1;
        return *this;
    }


    bool operator <(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y < cell.y;
        }
        else
        {
            return x < cell.x;
        }
    }

    bool operator >(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y > cell.y;
        }
        else
        {
            return x > cell.x;
        }
    }

    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }
};

struct myCompare
{
    bool operator()(const int &a, const int &b)
    {
        return a < b;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

std::ostream &operator<<(std::ostream &os, const std::pair<const int, Cell> &pCell)
{
    os << pCell.first << "-" << pCell.second;
    return os;
}

int main()
{
    auto genKey = []()
    {
        return std::rand() % 10 + 100;
    };

    auto generate = []()
    {
        int n = std::rand() % 10 + 100;
        Cell cell{n, n};
        return cell;
    };

    //1) 构造空容器。
    std::map<int, Cell> map1;
    std::cout << "map1 is empty: " << map1.empty() << std::endl;
    for (size_t index = 0; index < 5; index++)
    {
        map1.insert({genKey(), generate()});
    }
    std::cout << std::endl;

    //2) 构造容器,使之拥有范围 [first, last) 的内容。
    std::map<int, Cell> map2(map1.begin(), map1.end());
    std::cout << "map2:    ";
    std::copy(map2.begin(), map2.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;
    // 逆序
    std::map<int, Cell, std::greater<int>> map8(map1.begin(), map1.end());
    std::cout << "map8:    ";
    std::copy(map8.begin(), map8.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    //3) 复制构造函数。构造容器,使之拥有 other 的内容副本。
    std::map<int, Cell> map3(map2);
    std::cout << "map3:    ";
    std::copy(map3.begin(), map3.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;
    std::map<int, Cell, std::greater<int>> map9(map8);
    std::cout << "map9:    ";
    std::copy(map9.begin(), map9.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    //4) 移动构造函数。用移动语义构造容器,使之拥有 other 的内容。
    std::map<int, Cell> map4(std::move(map2));
    std::cout << "map4:    ";
    std::copy(map4.begin(), map4.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << "map2(move) is empty: " << map2.empty() << std::endl;
    std::map<int, Cell, std::greater<int>> map10(std::move(map8));
    std::cout << "map10:   ";
    std::copy(map10.begin(), map10.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << "map8(move) is empty: " << map10.empty() << std::endl;
    std::cout << std::endl;

    //5) 构造容器,使之拥有 initializer_list init 的内容。
    std::map<int, Cell> map5({{genKey(), generate()}, {genKey(), generate()},
        {genKey(), generate()}, {genKey(), generate()}, {genKey(), generate()}});
    std::cout << "map5:    ";
    std::copy(map5.begin(), map5.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;
    std::map<int, Cell, std::greater<int>> map11({{genKey(), generate()}, {genKey(), generate()},
        {genKey(), generate()}, {genKey(), generate()}, {genKey(), generate()}});
    std::cout << "map11:   ";
    std::copy(map11.begin(), map11.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    //使用系统key比较函数
    std::map<int, Cell, std::greater<int>> map6({{genKey(), generate()}, {genKey(), generate()},
        {genKey(), generate()}, {genKey(), generate()}, {genKey(), generate()}});
    std::cout << "map6:    ";
    std::copy(map6.begin(), map6.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    //使用自定义key比较函数
    std::map<int, Cell, myCompare> map7({{genKey(), generate()}, {genKey(), generate()},
        {genKey(), generate()}, {genKey(), generate()}, {genKey(), generate()}});
    std::cout << "map7:    ";
    std::copy(map7.begin(), map7.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;
    return 0;
}

输出

 


http://www.niftyadmin.cn/n/354063.html

相关文章

建议熟知:2023谷歌新搜索规则!

谷歌作为全球最大的搜索引擎之一&#xff0c;不断更新和调整其搜索算法和规则&#xff0c;以提供更精准、高质量的搜索结果。2023年&#xff0c;谷歌搜索将迎来一系列新的搜索规则&#xff0c;同时&#xff0c;AI工具的快速发展也为谷歌搜索带来了全新的应用场景和可能性。 这…

Asp.net Core系列学习(1)

Asp.net Core 6系列学习 文章目录 Asp.net Core 6系列学习Asp.net Core 概述一、在 ASP.NET 4.x 和 ASP.NET Core 之间进行选择二、适用于服务器应用的 .NET 与 .NET Framework三、ASP.NET Core Web UI1.服务器和客户端呈现 UI 的优势和成本2.服务器呈现的 UI 四、可用的 ASP.N…

两种蚁狮群优化(Ant Lion Optimizer, ALO)实现及仿真实验——附代码

目录 蚁狮群优化算法介绍&#xff1a; 总结概括&#xff1a; ALO算法设计&#xff1a; 1.觅食的蚂蚁随机行走 2.设置陷阱 3.设置陷阱诱捕蚂蚁 4.捕获猎物重建洞穴 多目标MOALO算法 两种蚁狮算法求解效果 (1) ALO (2) MOALO Matlab代码分享&#xff1a; 蚁狮群优化算…

vue惊醒的知识

1、data&#xff0c;在data里定义的数据是响应式的&#xff0c;不在data里定义的不是响应式的&#xff0c;可以直接this........

嵌入式音视频开发面试过程遇到的问题!

前言&#xff1a; 今天继续给大家分享音视频面试过程会被常问到的一些问题&#xff01; 面试的具体题目&#xff1a; 1、说一下播放器的设计过程&#xff1a; 这里的话主要分以下几步完成&#xff1a; 开启一个线程进行解封装操作 , 这包括&#xff1a;读取音频、视频的压缩数据…

CodeForces.1810B.糖果.[中等][ifelse选择][注意输出格式]

题目描述&#xff1a; 解题思路&#xff1a; 题目解读&#xff1a; 初始状态只有一个糖果&#xff0c;即x1&#xff0c;给定想要获得的总糖果数y。 只能进行两种操作&#xff0c;分别是做2x-1和2x1。给出从 x1 到 目标数字 y 的操作步数和具体步骤。 示例1 从1到2&#xff…

函数式接口入门简介(存在疑问,求解答)

这里写目录标题 引子四种函数式接口-简单Demo四种函数式接口介绍函数式接口实战-代码对比关于Consumer赋值问题&#xff08;疑问&#xff0c;求解答&#xff09; 引子 只包含一个抽象方法的接口&#xff0c;就称为函数式接口。来源&#xff1a;java.util.function 我想在方法…

在Linux中为Simulink添加ROS自定义消息类型

在Linux中为Simulink添加ROS自定义消息类型 基于Matlab/Simulink的ROS自定义消息类型的添加方法 ROS与Simulink联合仿真(三):自定义Message 1、下载 ROS Toolbox Interface for ROS Custom Messages 将 roscustommsg.mlpkginstall 文件放入 MATLAB 工作空间 双击 roscustommsg…