c++ 11标准模板(STL) std::map(九)
定义于头文件<map>
template< class Key, | (1) | |
namespace pmr { template <class Key, class T, class Compare = std::less<Key>> | (2) | (C++17 起) |
std::map
是有序键值对容器,它的元素的键是唯一的。用比较函数 Compare
排序键。搜索、移除和插入操作拥有对数复杂度。 map 通常实现为红黑树。
在每个标准库使用比较 (Compare) 概念的位置,以等价关系检验唯一性。不精确而言,若二个对象 a
与 b
互相比较不小于对方 : !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) 检查 lhs
与 rhs
的内容是否相等,即它们是否拥有相同数量的元素且 lhs
中每个元素与 rhs
的同位置元素比较相等。
3-6) 按字典序比较 lhs
与 rhs
的内容。由等价于 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) 若 lhs
与 rhs
的大小不同则为常数,否则与容器大小成线性
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 算法。交换 lhs
与 rhs
的内容。调用 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 > | (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 ,否则为 falsestd::cout << "map1 == map1 : " << (map1 == map2) << std::endl;//2) 若容器内容不相等则为 true ,否则为 falsestd::cout << "map1 != map1 : " << (map1 != map2) << std::endl;//3) 若 lhs 的内容按字典序小于 rhs 的内容则为 true ,否则为 falsestd::cout << "map1 < map1 : " << (map1 < map2) << std::endl;//4) 若 lhs 的内容按字典序小于或等于 rhs 的内容则为 true ,否则为 falsestd::cout << "map1 <= map1 : " << (map1 <= map2) << std::endl;//5) 若 lhs 的内容按字典序大于 rhs 的内容则为 true ,否则为 falsestd::cout << "map1 > map1 : " << (map1 > map2) << std::endl;//6) 若 lhs 的内容按字典序大于或等于 rhs 的内容则为 true ,否则为 falsestd::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;
}
输出
相关文章:

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, clas…...
深入探索chatGPT插件:SceneXplain,Wolfram,和AppyPieAIAppBuilder
1. 引言 在这个日益依赖技术的世界中,AI插件已经成为了我们日常生活和工作的重要组成部分。在这篇博客中,我们将深入探索三个强大的AI插件:SceneXplain,Wolfram,和AppyPieAIAppBuilder,了解他们的功能&…...

华为OD机试真题B卷 Java 实现【停车场车辆统计】,附详细解题思路
一、题目描述 特定大小的停车场,数组cars[]表示,其中1表示有车,0表示没车。 车辆大小不一,小车占一个车位(长度1),货车占两个车位(长度2),卡车占三个车位&a…...

第二章:MySQL环境搭建
第二章:MySQL环境搭建 2.1:MySQL的下载、安装、配置 MySQL的四大版本 MySQL Community Server社区版本:开源免费、自由下载,但不提供官方技术支持,适用于大多数普通用户。MySQL Enterprise Edition企业版本࿱…...
生产环境之负载均衡LVS+keepalived方案(2)_LVS介绍
LVS简介 LVS(Linux Virtual Server)即Linux虚拟服务器,linux内核2.6.X之后的版本默认已集成了LVS模块(内核模块名为:ipvs),实现了基于传输层的请求负载均衡调度方案,LVS支持的工作模…...
【parsel】------- PYTHON爬虫基础4
parsel 这个库可以对 HTML 和 XML 进行解析,并支持使用 XPath 和 CSS Selector 对内容进行提取和修改,同时它还融合了正则表达式提取的功能。 内容目录 from parsel import Selector提取节点提取 class 包含 item-0 的节点 提取文本获取提取到的所有 li…...

MySQL数据库从入门到精通学习第8天(表数据的查询)
表数据的查询 基本查询语句单表查询聚合函数查询多表连接查询子查询合并查询结果定义表和字段的别名使用正则表达式查询 基本查询语句 SELECT 语句非常的强大,是最常用的查询语句。他具有一个固定的格式,如下: SELECT 查询的内容 FROM 数据…...

什么是IPAM?如何使用IPAM来管理IP地址和DHCP?
在计算机网络中,IPAM(IP Address Management)是一种用于管理IP地址和DHCP(Dynamic Host Configuration Protocol)的工具或系统。IPAM旨在简化和集中管理IP地址分配、子网划分和DHCP配置等任务。本文将详细介绍IPAM的概…...

PCIE学习
目录 一、PCIE结构1、层次结构2、数据包TLPDLLP PCIE寄存器配置1、基址寄存器的作用2、基址寄存器的位置 三、PCIE读取数据 一、PCIE结构 1、层次结构 绝大多数的总线或者接口,都是采用分层实现的。PCIe也不例外,它的层次结构如下: PCIe定…...

商业智力,Social焕新|数说故事重磅发布“SocialGPT”,国内首个专注Social领域的商业大模型
AGI时代的到来,市场风云变幻,世界正在经历着一场技术革命的颠覆性洗礼。 2023年6月6日6时,数说故事正式对外发布数说故事“SocialGPT”,国内首个专注Social领域的商业大模型。数说故事“SocialGPT”大家昵称它为“社牛”大模型&a…...

STM32HAL库RS485-ModBus协议控制伺服电机
STM32HAL库RS485-ModBus协议控制伺服电机 一个月前,接手了一个学长的毕设小车,小车采用rs485通信的modbus协议驱动轮毂电机,与往常我学习的pwm控制电机方法大相径庭,在这里以这篇博客记录下该学习过程。 小车主要架构 电机型号 …...

【医学图像】图像分割系列.3 (uncertainty)
介绍几篇使用不确定性引导的医学图像分割论文:UA-MT(MICCAI2019),SSL4MIS(MICCAI2021),UG-MCL(AIIM2022). Uncertainty-aware Self-ensembling Model for Semi-supervise…...
Java有线程安全的set吗?
在Java中,有线程安全的Set实现。一个常用的线程安全的Set实现是ConcurrentSkipListSet。ConcurrentSkipListSet是一个有序的集合,基于跳表(SkipList)的数据结构实现。它提供了线程安全的操作,并且具有较好的性能。 接下来笔者用一段简单的Jav…...

《HelloGitHub》第 86 期
兴趣是最好的老师,HelloGitHub 让你对编程感兴趣! 简介 HelloGitHub 分享 GitHub 上有趣、入门级的开源项目。 https://github.com/521xueweihan/HelloGitHub 这里有实战项目、入门教程、黑科技、开源书籍、大厂开源项目等,涵盖多种编程语言 …...

LDGRB-01 3BSE013177R1 将数字输入和继电器输出结合
LDGRB-01 3BSE013177R1包的一部分是全面的通信选项,包括Modbus主/从或CS31,这种产品很少提供。128kB的用户内存和0.1秒/指令的程序处理时间只是AC500-eCo令人印象深刻的性能的两个例子。除了与现有AC500系列的互操作性,AC500-eCo系统还使用基…...

手动计算校正年龄、性别后的标准化死亡率 (SMR)
分析队列人群有无死亡人数超额,通常应用标准人群死亡率来校正,即刻观察到中的实际死亡数(D)与定一个标准的死亡人数(E),D与E之比称为死亡比(standarized Mortality ratio,…...

Java组合模式:构建多层次公司组织架构
在现实生活中,常常会遇到用树形结构组织的一些场景,比如国家省市,学校班级,文件目录,分级导航菜单,以及典型的公司组织架构,整个层次结构自顶向下呈现一颗倒置的树。这种树形结构在面向对象的世…...

Langchain-ChatGLM:基于本地知识库问答
文章目录 ChatGLM与Langchain简介ChatGLM-6B简介ChatGLM-6B是什么ChatGLM-6B具备的能力ChatGLM-6B具备的应用 Langchain简介Langchain是什么Langchain的核心模块Langchain的应用场景 ChatGLM与Langchain项目介绍知识库问答实现步骤ChatGLM与Langchain项目特点 项目主体结构项目…...
设计模式十 适配器模式
适配器模式 适配器模式是一种结构型设计模式。作用:当接口无法和类匹配到一起工作时,通过适配器将接口变换成可以和类匹配到一起的接口。(注:适配器模式主要解决接口兼容性问题) 适配器的优点与缺点: 优…...

1.6 初探JdbcTemplate操作
一、JdbcTemplate案例演示 1、创建数据库与表 (1)创建数据库 执行命令:CREATE DATABASE simonshop DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 或者利用菜单方式创建数据库 - simonshop 打开数据库simonshop &#x…...
Java 语言特性(面试系列1)
一、面向对象编程 1. 封装(Encapsulation) 定义:将数据(属性)和操作数据的方法绑定在一起,通过访问控制符(private、protected、public)隐藏内部实现细节。示例: public …...

基于距离变化能量开销动态调整的WSN低功耗拓扑控制开销算法matlab仿真
目录 1.程序功能描述 2.测试软件版本以及运行结果展示 3.核心程序 4.算法仿真参数 5.算法理论概述 6.参考文献 7.完整程序 1.程序功能描述 通过动态调整节点通信的能量开销,平衡网络负载,延长WSN生命周期。具体通过建立基于距离的能量消耗模型&am…...

练习(含atoi的模拟实现,自定义类型等练习)
一、结构体大小的计算及位段 (结构体大小计算及位段 详解请看:自定义类型:结构体进阶-CSDN博客) 1.在32位系统环境,编译选项为4字节对齐,那么sizeof(A)和sizeof(B)是多少? #pragma pack(4)st…...

Debian系统简介
目录 Debian系统介绍 Debian版本介绍 Debian软件源介绍 软件包管理工具dpkg dpkg核心指令详解 安装软件包 卸载软件包 查询软件包状态 验证软件包完整性 手动处理依赖关系 dpkg vs apt Debian系统介绍 Debian 和 Ubuntu 都是基于 Debian内核 的 Linux 发行版ÿ…...
可靠性+灵活性:电力载波技术在楼宇自控中的核心价值
可靠性灵活性:电力载波技术在楼宇自控中的核心价值 在智能楼宇的自动化控制中,电力载波技术(PLC)凭借其独特的优势,正成为构建高效、稳定、灵活系统的核心解决方案。它利用现有电力线路传输数据,无需额外布…...
pam_env.so模块配置解析
在PAM(Pluggable Authentication Modules)配置中, /etc/pam.d/su 文件相关配置含义如下: 配置解析 auth required pam_env.so1. 字段分解 字段值说明模块类型auth认证类模块,负责验证用户身份&am…...
多模态商品数据接口:融合图像、语音与文字的下一代商品详情体验
一、多模态商品数据接口的技术架构 (一)多模态数据融合引擎 跨模态语义对齐 通过Transformer架构实现图像、语音、文字的语义关联。例如,当用户上传一张“蓝色连衣裙”的图片时,接口可自动提取图像中的颜色(RGB值&…...

Mac软件卸载指南,简单易懂!
刚和Adobe分手,它却总在Library里给你写"回忆录"?卸载的Final Cut Pro像电子幽灵般阴魂不散?总是会有残留文件,别慌!这份Mac软件卸载指南,将用最硬核的方式教你"数字分手术"࿰…...

CocosCreator 之 JavaScript/TypeScript和Java的相互交互
引擎版本: 3.8.1 语言: JavaScript/TypeScript、C、Java 环境:Window 参考:Java原生反射机制 您好,我是鹤九日! 回顾 在上篇文章中:CocosCreator Android项目接入UnityAds 广告SDK。 我们简单讲…...
Angular微前端架构:Module Federation + ngx-build-plus (Webpack)
以下是一个完整的 Angular 微前端示例,其中使用的是 Module Federation 和 npx-build-plus 实现了主应用(Shell)与子应用(Remote)的集成。 🛠️ 项目结构 angular-mf/ ├── shell-app/ # 主应用&…...