当前位置: 首页 > news >正文

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) 的要求。

 

观察器

返回用于比较键的函数

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

华为OD机试真题B卷 Java 实现【停车场车辆统计】,附详细解题思路

一、题目描述 特定大小的停车场&#xff0c;数组cars[]表示&#xff0c;其中1表示有车&#xff0c;0表示没车。 车辆大小不一&#xff0c;小车占一个车位&#xff08;长度1&#xff09;&#xff0c;货车占两个车位&#xff08;长度2&#xff09;&#xff0c;卡车占三个车位&a…...

第二章:MySQL环境搭建

第二章&#xff1a;MySQL环境搭建 2.1&#xff1a;MySQL的下载、安装、配置 MySQL的四大版本 MySQL Community Server社区版本&#xff1a;开源免费、自由下载&#xff0c;但不提供官方技术支持&#xff0c;适用于大多数普通用户。MySQL Enterprise Edition企业版本&#xff1…...

生产环境之负载均衡LVS+keepalived方案(2)_LVS介绍

LVS简介 LVS&#xff08;Linux Virtual Server&#xff09;即Linux虚拟服务器&#xff0c;linux内核2.6.X之后的版本默认已集成了LVS模块&#xff08;内核模块名为&#xff1a;ipvs&#xff09;&#xff0c;实现了基于传输层的请求负载均衡调度方案&#xff0c;LVS支持的工作模…...

【parsel】------- PYTHON爬虫基础4

parsel 这个库可以对 HTML 和 XML 进行解析&#xff0c;并支持使用 XPath 和 CSS Selector 对内容进行提取和修改&#xff0c;同时它还融合了正则表达式提取的功能。 内容目录 from parsel import Selector提取节点提取 class 包含 item-0 的节点 提取文本获取提取到的所有 li…...

MySQL数据库从入门到精通学习第8天(表数据的查询)

表数据的查询 基本查询语句单表查询聚合函数查询多表连接查询子查询合并查询结果定义表和字段的别名使用正则表达式查询 基本查询语句 SELECT 语句非常的强大&#xff0c;是最常用的查询语句。他具有一个固定的格式&#xff0c;如下&#xff1a; SELECT 查询的内容 FROM 数据…...

什么是IPAM?如何使用IPAM来管理IP地址和DHCP?

在计算机网络中&#xff0c;IPAM&#xff08;IP Address Management&#xff09;是一种用于管理IP地址和DHCP&#xff08;Dynamic Host Configuration Protocol&#xff09;的工具或系统。IPAM旨在简化和集中管理IP地址分配、子网划分和DHCP配置等任务。本文将详细介绍IPAM的概…...

PCIE学习

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

商业智力,Social焕新|数说故事重磅发布“SocialGPT”,国内首个专注Social领域的商业大模型

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

STM32HAL库RS485-ModBus协议控制伺服电机

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

【医学图像】图像分割系列.3 (uncertainty)

介绍几篇使用不确定性引导的医学图像分割论文&#xff1a;UA-MT&#xff08;MICCAI2019&#xff09;&#xff0c;SSL4MIS&#xff08;MICCAI2021&#xff09;&#xff0c;UG-MCL&#xff08;AIIM2022&#xff09;. Uncertainty-aware Self-ensembling Model for Semi-supervise…...

Java有线程安全的set吗?

在Java中&#xff0c;有线程安全的Set实现。一个常用的线程安全的Set实现是ConcurrentSkipListSet。ConcurrentSkipListSet是一个有序的集合&#xff0c;基于跳表(SkipList)的数据结构实现。它提供了线程安全的操作&#xff0c;并且具有较好的性能。 接下来笔者用一段简单的Jav…...

《HelloGitHub》第 86 期

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

LDGRB-01 3BSE013177R1 将数字输入和继电器输出结合

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

手动计算校正年龄、性别后的标准化死亡率 (SMR)

分析队列人群有无死亡人数超额&#xff0c;通常应用标准人群死亡率来校正&#xff0c;即刻观察到中的实际死亡数&#xff08;D&#xff09;与定一个标准的死亡人数&#xff08;E&#xff09;&#xff0c;D与E之比称为死亡比&#xff08;standarized Mortality ratio&#xff0c…...

Java组合模式:构建多层次公司组织架构

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

Langchain-ChatGLM:基于本地知识库问答

文章目录 ChatGLM与Langchain简介ChatGLM-6B简介ChatGLM-6B是什么ChatGLM-6B具备的能力ChatGLM-6B具备的应用 Langchain简介Langchain是什么Langchain的核心模块Langchain的应用场景 ChatGLM与Langchain项目介绍知识库问答实现步骤ChatGLM与Langchain项目特点 项目主体结构项目…...

设计模式十 适配器模式

适配器模式 适配器模式是一种结构型设计模式。作用&#xff1a;当接口无法和类匹配到一起工作时&#xff0c;通过适配器将接口变换成可以和类匹配到一起的接口。&#xff08;注&#xff1a;适配器模式主要解决接口兼容性问题&#xff09; 适配器的优点与缺点&#xff1a; 优…...

1.6 初探JdbcTemplate操作

一、JdbcTemplate案例演示 1、创建数据库与表 &#xff08;1&#xff09;创建数据库 执行命令&#xff1a;CREATE DATABASE simonshop DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 或者利用菜单方式创建数据库 - simonshop 打开数据库simonshop &#x…...

Java 语言特性(面试系列1)

一、面向对象编程 1. 封装&#xff08;Encapsulation&#xff09; 定义&#xff1a;将数据&#xff08;属性&#xff09;和操作数据的方法绑定在一起&#xff0c;通过访问控制符&#xff08;private、protected、public&#xff09;隐藏内部实现细节。示例&#xff1a; public …...

基于距离变化能量开销动态调整的WSN低功耗拓扑控制开销算法matlab仿真

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

练习(含atoi的模拟实现,自定义类型等练习)

一、结构体大小的计算及位段 &#xff08;结构体大小计算及位段 详解请看&#xff1a;自定义类型&#xff1a;结构体进阶-CSDN博客&#xff09; 1.在32位系统环境&#xff0c;编译选项为4字节对齐&#xff0c;那么sizeof(A)和sizeof(B)是多少&#xff1f; #pragma pack(4)st…...

Debian系统简介

目录 Debian系统介绍 Debian版本介绍 Debian软件源介绍 软件包管理工具dpkg dpkg核心指令详解 安装软件包 卸载软件包 查询软件包状态 验证软件包完整性 手动处理依赖关系 dpkg vs apt Debian系统介绍 Debian 和 Ubuntu 都是基于 Debian内核 的 Linux 发行版&#xff…...

可靠性+灵活性:电力载波技术在楼宇自控中的核心价值

可靠性灵活性&#xff1a;电力载波技术在楼宇自控中的核心价值 在智能楼宇的自动化控制中&#xff0c;电力载波技术&#xff08;PLC&#xff09;凭借其独特的优势&#xff0c;正成为构建高效、稳定、灵活系统的核心解决方案。它利用现有电力线路传输数据&#xff0c;无需额外布…...

pam_env.so模块配置解析

在PAM&#xff08;Pluggable Authentication Modules&#xff09;配置中&#xff0c; /etc/pam.d/su 文件相关配置含义如下&#xff1a; 配置解析 auth required pam_env.so1. 字段分解 字段值说明模块类型auth认证类模块&#xff0c;负责验证用户身份&am…...

多模态商品数据接口:融合图像、语音与文字的下一代商品详情体验

一、多模态商品数据接口的技术架构 &#xff08;一&#xff09;多模态数据融合引擎 跨模态语义对齐 通过Transformer架构实现图像、语音、文字的语义关联。例如&#xff0c;当用户上传一张“蓝色连衣裙”的图片时&#xff0c;接口可自动提取图像中的颜色&#xff08;RGB值&…...

Mac软件卸载指南,简单易懂!

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

CocosCreator 之 JavaScript/TypeScript和Java的相互交互

引擎版本&#xff1a; 3.8.1 语言&#xff1a; JavaScript/TypeScript、C、Java 环境&#xff1a;Window 参考&#xff1a;Java原生反射机制 您好&#xff0c;我是鹤九日&#xff01; 回顾 在上篇文章中&#xff1a;CocosCreator Android项目接入UnityAds 广告SDK。 我们简单讲…...

Angular微前端架构:Module Federation + ngx-build-plus (Webpack)

以下是一个完整的 Angular 微前端示例&#xff0c;其中使用的是 Module Federation 和 npx-build-plus 实现了主应用&#xff08;Shell&#xff09;与子应用&#xff08;Remote&#xff09;的集成。 &#x1f6e0;️ 项目结构 angular-mf/ ├── shell-app/ # 主应用&…...