当前位置: 首页 > 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…...

Vim 调用外部命令学习笔记

Vim 外部命令集成完全指南 文章目录 Vim 外部命令集成完全指南核心概念理解命令语法解析语法对比 常用外部命令详解文本排序与去重文本筛选与搜索高级 grep 搜索技巧文本替换与编辑字符处理高级文本处理编程语言处理其他实用命令 范围操作示例指定行范围处理复合命令示例 实用技…...

Chapter03-Authentication vulnerabilities

文章目录 1. 身份验证简介1.1 What is authentication1.2 difference between authentication and authorization1.3 身份验证机制失效的原因1.4 身份验证机制失效的影响 2. 基于登录功能的漏洞2.1 密码爆破2.2 用户名枚举2.3 有缺陷的暴力破解防护2.3.1 如果用户登录尝试失败次…...

【Linux】shell脚本忽略错误继续执行

在 shell 脚本中&#xff0c;可以使用 set -e 命令来设置脚本在遇到错误时退出执行。如果你希望脚本忽略错误并继续执行&#xff0c;可以在脚本开头添加 set e 命令来取消该设置。 举例1 #!/bin/bash# 取消 set -e 的设置 set e# 执行命令&#xff0c;并忽略错误 rm somefile…...

利用ngx_stream_return_module构建简易 TCP/UDP 响应网关

一、模块概述 ngx_stream_return_module 提供了一个极简的指令&#xff1a; return <value>;在收到客户端连接后&#xff0c;立即将 <value> 写回并关闭连接。<value> 支持内嵌文本和内置变量&#xff08;如 $time_iso8601、$remote_addr 等&#xff09;&a…...

【人工智能】神经网络的优化器optimizer(二):Adagrad自适应学习率优化器

一.自适应梯度算法Adagrad概述 Adagrad&#xff08;Adaptive Gradient Algorithm&#xff09;是一种自适应学习率的优化算法&#xff0c;由Duchi等人在2011年提出。其核心思想是针对不同参数自动调整学习率&#xff0c;适合处理稀疏数据和不同参数梯度差异较大的场景。Adagrad通…...

【Java学习笔记】Arrays类

Arrays 类 1. 导入包&#xff1a;import java.util.Arrays 2. 常用方法一览表 方法描述Arrays.toString()返回数组的字符串形式Arrays.sort()排序&#xff08;自然排序和定制排序&#xff09;Arrays.binarySearch()通过二分搜索法进行查找&#xff08;前提&#xff1a;数组是…...

Cesium1.95中高性能加载1500个点

一、基本方式&#xff1a; 图标使用.png比.svg性能要好 <template><div id"cesiumContainer"></div><div class"toolbar"><button id"resetButton">重新生成点</button><span id"countDisplay&qu…...

postgresql|数据库|只读用户的创建和删除(备忘)

CREATE USER read_only WITH PASSWORD 密码 -- 连接到xxx数据库 \c xxx -- 授予对xxx数据库的只读权限 GRANT CONNECT ON DATABASE xxx TO read_only; GRANT USAGE ON SCHEMA public TO read_only; GRANT SELECT ON ALL TABLES IN SCHEMA public TO read_only; GRANT EXECUTE O…...

sqlserver 根据指定字符 解析拼接字符串

DECLARE LotNo NVARCHAR(50)A,B,C DECLARE xml XML ( SELECT <x> REPLACE(LotNo, ,, </x><x>) </x> ) DECLARE ErrorCode NVARCHAR(50) -- 提取 XML 中的值 SELECT value x.value(., VARCHAR(MAX))…...

基于Docker Compose部署Java微服务项目

一. 创建根项目 根项目&#xff08;父项目&#xff09;主要用于依赖管理 一些需要注意的点&#xff1a; 打包方式需要为 pom<modules>里需要注册子模块不要引入maven的打包插件&#xff0c;否则打包时会出问题 <?xml version"1.0" encoding"UTF-8…...