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

news2024/5/18 17:14:38

定义于头文件<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) 的要求。

 

观察器

返回用于比较键的函数

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

key_compare key_comp() const;

 返回用于比较关键的函数对象,它是此容器构造函数参数 comp 的副本。

参数

(无)

返回值

比较关键的函数对象。

复杂度

常数

返回用于在value_type类型的对象中比较键的函数

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

std::map::value_compare value_comp() const;

 返回比较 std::map::value_type (关键-值 pair )对象的函数对象,它用 key_comp 比较 pair 的第一组分。

参数

(无)

返回值

比较值的函数对象。

复杂度

常数。

非成员函数

按照字典顺序比较 map 中的值

operator==,!=,<,<=,>,>=(std::map)
template< class Key, class T, class Compare, class Alloc >

bool operator==( const std::map<Key,T,Compare,Alloc>& lhs,

                 const std::map<Key,T,Compare,Alloc>& rhs );
(1)
template< class Key, class T, class Compare, class Alloc >

bool operator!=( const std::map<Key,T,Compare,Alloc>& lhs,

                 const std::map<Key,T,Compare,Alloc>& rhs );
(2)
template< class Key, class T, class Compare, class Alloc >

bool operator<( const std::map<Key,T,Compare,Alloc>& lhs,

                const std::map<Key,T,Compare,Alloc>& rhs );
(3)
template< class Key, class T, class Compare, class Alloc >

bool operator<=( const std::map<Key,T,Compare,Alloc>& lhs,

                 const std::map<Key,T,Compare,Alloc>& rhs );
(4)
template< class Key, class T, class Compare, class Alloc >

bool operator>( const std::map<Key,T,Compare,Alloc>& lhs,

                const std::map<Key,T,Compare,Alloc>& rhs );
(5)
template< class Key, class T, class Compare, class Alloc >

bool operator>=( const std::map<Key,T,Compare,Alloc>& lhs,

                 const std::map<Key,T,Compare,Alloc>& rhs );
(6)

 比较二个容器的内容。

1-2) 检查 lhsrhs 的内容是否相等,即它们是否拥有相同数量的元素且 lhs 中每个元素与 rhs 的同位置元素比较相等。

3-6) 按字典序比较 lhsrhs 的内容。由等价于 std::lexicographical_compare 的函数进行比较。此比较忽略容器的定序 Compare 。

参数

lhs, rhs-要比较内容的容器
- 为使用重载 (1-2) , T, Key 必须满足可相等比较 (EqualityComparable) 的要求。
- 为使用重载 (3-6) , Key 必须满足可小于比较 (LessThanComparable) 的要求。顺序关系必须建立全序。

返回值

1) 若容器内容相等则为 true ,否则为 false

2) 若容器内容不相等则为 true ,否则为 false

3) 若 lhs 的内容按字典序小于 rhs 的内容则为 true ,否则为 false

4) 若 lhs 的内容按字典序小于等于 rhs 的内容则为 true ,否则为 false

5) 若 lhs 的内容按字典序大于 rhs 的内容则为 true ,否则为 false

6) 若 lhs 的内容按字典序大于等于 rhs 的内容则为 true ,否则为 false

复杂度

1-2) 若 lhsrhs 的大小不同则为常数,否则与容器大小成线性

3-6) 与容器大小成线性

特化 std::swap 算法

std::swap(std::map)
template< class Key, class T, class Compare, class Alloc >

void swap( map<Key,T,Compare,Alloc>& lhs,

           map<Key,T,Compare,Alloc>& rhs );
(C++17 前)
template< class Key, class T, class Compare, class Alloc >

void swap( map<Key,T,Compare,Alloc>& lhs,

           map<Key,T,Compare,Alloc>& rhs ) noexcept(/* see below */);
(C++17 起)

 为 std::map 特化 std::swap 算法。交换 lhsrhs 的内容。调用 lhs.swap(rhs) 。

参数

lhs, rhs-要交换内容的容器

返回值

(无)

复杂度

常数。

异常

noexcept 规定:  

noexcept(noexcept(lhs.swap(rhs)))

(C++17 起)

擦除所有满足特定判别标准的元素

std::erase_if(std::map)

template< class Key, class T, class Compare, class Alloc, class Pred >
void erase_if(std::map<Key,T,Compare,Alloc>& c, Pred pred);

(1)(C++20 起)

 从容器中擦除所有满足谓词 pred 的元素。等价于

for (auto i = c.begin(), last = c.end(); i != last; ) {
  if (pred(*i)) {
    i = c.erase(i);
  } else {
    ++i;
  }
}

参数

c-要从中擦除的元素
pred-若应该擦除元素则对它返回 true 的谓词

复杂度

线性。

调用示例

#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()
{
    std::cout << std::boolalpha;

    std::mt19937 g{std::random_device{}()};
    srand((unsigned)time(NULL));

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

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

    std::map<int, Cell> map1;
    for (size_t index = 0; index < 5; index++)
    {
        //插入以给定的 args 原位构造的新元素到容器。
        map1.emplace(genKey(), generate());
    }
    std::cout << "map1:    ";
    std::copy(map1.begin(), map1.end(),
              std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;

    std::map<int, Cell> map2;
    for (size_t index = 0; index < 5; index++)
    {
        //插入以给定的 args 原位构造的新元素到容器。
        map2.emplace(genKey(), generate());
    }
    std::cout << "map2:    ";
    std::copy(map2.begin(), map2.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    //比较二个容器的内容。
    //1) 若容器内容相等则为 true ,否则为 false
    std::cout << "map1 == map1 :  " << (map1 == map2) << std::endl;
    //2) 若容器内容不相等则为 true ,否则为 false
    std::cout << "map1 != map1 :  " << (map1 != map2) << std::endl;
    //3) 若 lhs 的内容按字典序小于 rhs 的内容则为 true ,否则为 false
    std::cout << "map1 <  map1 :  " << (map1 <  map2) << std::endl;
    //4) 若 lhs 的内容按字典序小于或等于 rhs 的内容则为 true ,否则为 false
    std::cout << "map1 <= map1 :  " << (map1 <= map2) << std::endl;
    //5) 若 lhs 的内容按字典序大于 rhs 的内容则为 true ,否则为 false
    std::cout << "map1 >  map1 :  " << (map1 >  map2) << std::endl;
    //6) 若 lhs 的内容按字典序大于或等于 rhs 的内容则为 true ,否则为 false
    std::cout << "map1 >= map1 :  " << (map1 >= map2) << std::endl;
    std::cout << std::endl;


    //返回用于比较关键的函数对象,它是此容器构造函数参数 comp 的副本。
    auto key_comp = map2.key_comp();
    std::cout << "key_comp(map1.begin()->first, map2.begin()->first) :    "
              << key_comp(map1.begin()->first, map2.begin()->first) << std::endl;
    std::cout << std::endl;

    auto value_comp = map2.value_comp();
    std::cout << "value_comp(*map1.begin(), *map2.begin()) :              "
              << value_comp(*map1.begin(), *map2.begin()) << std::endl;
    std::cout << std::endl;

    //为 std::map 特化 std::swap 算法。交换 lhs 与 rhs 的内容。调用 lhs.swap(rhs) 。
    std::swap(map1, map2);

    std::cout << "map1:    ";
    std::copy(map1.begin(), map1.end(),
              std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;

    std::cout << "map2:    ";
    std::copy(map2.begin(), map2.end(),
              std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;
    return 0;
}

输出

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/592058.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

国产系统:麒麟之人大金仓数据库部署

一、基本信息和资源 1.1 查看服务器信息 [root7PGxjKPL4 ~]# cat /etc/*release Kylin Linux Advanced Server release V10 (Sword) DISTRIB_IDKylin DISTRIB_RELEASEV10 DISTRIB_CODENAMEjuniper DISTRIB_DESCRIPTION"Kylin V10" DISTRIB_KYLIN_RELEASEV10 DISTRI…

Springboot异步执行

异步执行 1.基于Async注解的方式在异步的方法上加 Async注解&#xff0c;调用接口后基于Async注解的方式优缺点: 2.使用 CompletableFuture 实现异步任务在实现类中创建CompletableFuture 类型的方法优缺点: 3.使用 TaskExecutor 实现异步任务优缺点: 1.基于Async注解的方式 As…

电子企业使用MES管理系统有没有弊端

随着制造业的不断现代化和数字化&#xff0c;越来越多的电子企业开始使用MES生产管理系统。电子企业MES系统是一种用于监控和管理制造业生产过程的软件&#xff0c;能够帮助企业提高生产效率、降低成本、提高质量和灵活性。然而&#xff0c;电子企业使用MES管理系统也存在一些弊…

Autohotkey按键映射

文章目录 功能前缀鼠标按键键盘按键虚拟键码和扫描码实操 功能前缀 尽管在初步使用中已经对常见热键做了说明&#xff0c;但为了本文的完整性&#xff0c;这里还是重新表述一下 #!^<^>!winAltCtrlShiftAlt Gr 其中&#xff0c;<, >为修饰符&#xff0c;用于区分…

使用不同类型注释的小肠路径跟踪深度强化学习

文章目录 Deep Reinforcement Learning for Small Bowel Path Tracking Using Different Types of Annotations摘要本文方法环境stateActionreward 实验结果 Deep Reinforcement Learning for Small Bowel Path Tracking Using Different Types of Annotations 摘要 小肠路径…

令人惊艳的六大算法(哈希表、分治算法、动态规划算法、贪心算法、回溯算法、图论算法)

当谈到计算机科学时&#xff0c;算法是一个重要的话题&#xff0c;因为它们能帮助解决很多问题。有些算法尤其令人惊艳&#xff0c;因为它们不仅高效&#xff0c;而且有着惊人的表现。在这篇文章中&#xff0c;我将分享一些我认为令人惊艳的高效算法。 一、哈希表 哈希表是一种…

通用寄存器-汇编复习(1)

弄清寄存器表达,原理和配件及汇编实验验证。 往期文章: 汇编语言基础-汇编复习(0)_luozhonghua2000的博客-CSDN博客 一个典型的 CPU(此处讨论的不是某一具体的 CPU)由运算器、控制器、寄存器(CPU工作原理)等器件构成,这些器件靠内部总线相连。前一章所说的总线,相对于 CP…

4、USB协议学习:USB的数据包结构

文章目录 数据包结构包(Packet)PID令牌包SETUP&OUT&IN令牌包SOF令牌包 数据包握手包ACK 握手包NAK 握手包 事务(Transaction)Setup事务OUT事务IN事务 传输(Transfer)控制传输中断传输批量传输同步传输/等时传输 端点 数据包结构 USB的通讯数据由多个传输组成&#xff0…

Docker 构建多架构 ARM、x86 AMD image镜像

在当今的计算环境中&#xff0c;各种异构计算设备和平台层出不穷&#xff0c;如何保证应用程序能够在不同的平台和设备上顺利运行&#xff0c;已成为亟待解决的问题。 以一款应用程序为例&#xff0c;它可能需要在 ARM、x86 或 s390x 等不同架构的设备上运行。由于这些设备所使…

Linux—实操篇:关机,重启和用户登录注销

1、关机和重启命令 1.1、基本介绍 shutdown -h now 立即关机 shutdown -h 1 一分钟后关机 shutdown -r now 立即重启 halt 立即关机&#xff0c;作用和上面一样 reboot 立即重启 sync 把内存数据同步到磁盘 注意&#xff1a;仅输入shutdown 默认执行&#xff08;shutdow…

Go开发学习 | 如何快速读取json/yaml/ini等格式的配置文件使用示例

欢迎关注「全栈工程师修炼指南」公众号 点击 &#x1f447; 下方卡片 即可关注我哟! 设为「星标⭐」每天带你 基础入门 到 进阶实践 再到 放弃学习&#xff01; “ 花开堪折直须折&#xff0c;莫待无花空折枝。 ” 作者主页&#xff1a;[ https://www.weiyigeek.top ] 博客&…

docker 数据持久化

目录 一、将本地目录直接映射到容器里&#xff08;运行成容器时候进行映射&#xff09; 二、数据卷模式 1、创建数据卷 2、查看数据卷列表&#xff0c;有哪些数据卷 3、查看某个数据卷 4、容器目录挂载到数据卷 5、数据卷的优势&#xff1a;多个容器共享一个数据卷 默认…

元宇宙应用领域-教育

教育是一个国家发展的基础&#xff0c;在科技发展的时代&#xff0c;元宇宙将会帮助教育行业实现跨越式发展。 元宇宙与教育的结合将会对传统的教学模式带来翻天覆地的变化。它能将线上教学、线下体验、远程互动等优势集于一身&#xff0c;也能把教师从繁重的重复劳动中解放出…

贝叶斯伪标签:鲁棒高效半监督分割的期望最大化

文章目录 Bayesian Pseudo Labels: Expectation Maximization for Robust and Efficient Semi-supervised Segmentation摘要作为期望最大化的伪标签基于变分推理的伪标签的推广实验结果 Bayesian Pseudo Labels: Expectation Maximization for Robust and Efficient Semi-super…

Qt上位机开发-学习记录(一)

一、Qt的安装 下载Qt : https://download.qt.io/ 进入archive/qt/&#xff0c;目前5.14版本下&#xff0c;有直接exe安装的版本&#xff0c;就直接下载 qt-opensource-windows-x86-5.14.2.exe安装Qt : 默认安装&#xff0c;过程中可以先全选 二、新建项目 选择Appliation-&g…

静态代码块、动态代码块、构造方法

类与对象 类&#xff1a;描述事物属性和行为 属性&#xff1a;私有化 行为&#xff1a;公开化 对象&#xff1a;就是类的一个具体实例 代码块&#xff1a; 静态代码块 发生在创建对象之前--时机 随着类的加载而加载 构造代码块 发生在创建对象之前&a…

解决小程序富文本显示视频问题

目录 1. 首先用小程序原生的 rich-text 肯定是不行的&#xff0c;它video的HTML节点和属性都不支持的 2. 采用安装插件的方法去处理&#xff08;强烈推荐&#xff1a;mp-html&#xff0c;可用于多端&#xff09; 3. 引入 4. 使用 5. 效果 1. 首先用小程序原生的 rich-text…

NLP实战:中文文本分类-Pytorch实现

目录 一、准备工作 1.任务说明 2.加载数据 二、数据预处理 1.构建词典 2.生成数据批次和迭代器 三、模型构建 1. 搭建模型 2.初始化模型 3. 定义训练与评估函数 四、训练模型 1. 拆分数据集并运行模型 顺便测试指定数据 五、总结 &#x1f368; 本文为[&#x1f517…

Benewake(北醒) 快速实现TF03-CAN与电脑通信操作说明

目录 一、前言二、工具准备三、连接方式3.1 串口通信连接方式3.2 CAN 通信连接方式 四、TF03 与电脑通信操作说明4.1 切换为CAN通信4.2 安装 USB_CAN TOOL 驱动4.3 CAN 通信下修改波特率 五、常见问题反馈5.1 V9.11 USB-CAN tool按照上述方案发送文件指令不成功的解决方案 一、…

跨平台开发的优势:ReactNative与小程序容器技术

结合React Native和小程序容器技术&#xff0c;开发者可以通过热重载和快速迭代提高开发效率&#xff0c;并实现统一的代码和逻辑&#xff0c;简化维护和升级过程。这种技术应用价值使得开发者能够更灵活地构建跨平台应用程序&#xff0c;并充分利用多个生态系统的优势。 Reac…