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

c++11 标准模板(STL)(std::unordered_multimap)(三)

定义于头文件 <unordered_map>
template<

    class Key,
    class T,
    class Hash = std::hash<Key>,
    class KeyEqual = std::equal_to<Key>,
    class Allocator = std::allocator< std::pair<const Key, T> >

> class unordered_multimap;
(1)(C++11 起)
namespace pmr {

    template <class Key, class T,
              class Hash = std::hash<Key>,
              class Pred = std::equal_to<Key>>
    using unordered_multimap = std::unordered_multimap<Key, T, Hash, Pred,
                                   std::pmr::polymorphic_allocator<std::pair<const Key,T>>>;

}
(2)(C++17 起)

 

unordered_multimap 是无序关联容器,支持等价的关键(一个 unordered_multimap 可含有每个关键值的多个副本)和将关键与另一类型的值关联。 unordered_multimap 类支持向前迭代器。搜索、插入和移除拥有平均常数时间复杂度。

元素在内部不以任何特定顺序排序,而是组织到桶中。元素被放进哪个桶完全依赖于其关键的哈希。这允许到单独元素的快速访问,因为哈希一旦计算,则它指代元素被放进的准确的桶。

不要求此容器的迭代顺序稳定(故例如 std::equal 不能用于比较二个 std::unordered_multimap ),除了关键比较等价(以 key_eq() 为比较器比较相等)的每组元素在迭代顺序中组成相接的子范围,它亦可用 equal_range() 访问。

成员函数

赋值给容器

std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>::operator=

unordered_multimap& operator=( const unordered_multimap& other );

(1)(C++11 起)

unordered_multimap& operator=( unordered_multimap&& other );

(2)(C++11 起)
(C++17 前)

unordered_multimap& operator=( unordered_multimap&& other ) noexcept(/* see below */);

(C++17 起)

unordered_multimap& operator=( std::initializer_list<value_type> ilist );

(3)(C++11 起)

 

替换容器内容。

1) 复制赋值运算符。以 other 的副本替换内容。若 std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value 为 true ,则以源分配器的副本替换目标分配器。若源分配器与目标分配器不比较相等,则用目标( *this )分配器销毁内存,然后在复制元素前用 other 的分配器分配。 (C++11 起).、

2) 移动赋值运算符。用移动语义以 other 的内容替换内容(即从 other 移动 other 中的数据到此容器)。之后 other 在合法但未指定的状态。若 std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value 为 true ,则用源分配器的副本替换目标分配器。若它为 false 且源与目标分配器不比较相等,则目标不能取走源内存的所有权,而必须单独移动赋值逐个元素,用自己的分配器按需分配额外的内存。任何情况下,原先在 *this 中的元素要么被销毁,要么以逐元素移动赋值替换。

3) 以 initializer_list ilist 所标识者替换内容。

参数

other-用作数据源的另一容器
ilist-用作数据源的 initializer_list

返回值

*this

复杂度

1) 与 *thisother 的大小成线性。

2) 与 *this 的大小成线性,除非分配器不比较相等且不传播,该情况下与 *thisother 的大小成线性。

3) 与 *thisilist 的大小成线性。

异常

2)noexcept 规定:  noexcept(std::allocator_traits<Allocator>::is_always_equal::value

&& std::is_nothrow_move_assignable<Hash>::value

&& std::is_nothrow_move_assignable<Pred>::value)
(C++17 起)

注意

容器移动赋值(重载 (2) )后,除非不兼容的分配器强制逐元素赋值,否则指向 other 的引用、指针和迭代器(除了尾迭代器)都保持合法,不过指代的元素现在在 *this 中。当前标准通过 §23.2.1[container.requirements.general]/12 中的总括陈述保证这点,而 LWG 2321 下正在考虑更直接的保证。

返回相关的分配器

std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>::get_allocator

allocator_type get_allocator() const;

(C++11 起)

返回与容器关联的分配器。

参数

(无)

返回值

关联的分配器。

复杂度

常数。

 

调用示例

#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <unordered_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<Cell, string> &pCell)
{os << pCell.first << "-" << pCell.second;return os;
}struct CHash
{size_t operator()(const Cell& cell) const{size_t thash = std::hash<int>()(cell.x) | std::hash<int>()(cell.y);
//        std::cout << "CHash: " << thash << std::endl;return thash;}
};struct CEqual
{bool operator()(const Cell &a, const Cell &b) const{return a.x == b.x && a.y == b.y;}
};int main()
{auto generate = [](){int n = std::rand() % 10 + 110;Cell cell{n, n};return std::pair<Cell, string>(cell, std::to_string(n));};std::unordered_multimap<Cell, string, CHash, CEqual> unordered_multimap1;while (unordered_multimap1.size() < 5){unordered_multimap1.insert(generate());}std::cout << "unordered_multimap1:   ";std::copy(unordered_multimap1.begin(), unordered_multimap1.end(),std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));std::cout << std::endl;//1) 复制赋值运算符。以 other 的副本替换内容。std::unordered_multimap<Cell, string, CHash, CEqual> unordered_multimap2 =  unordered_multimap1;std::cout << "unordered_multimap2:   ";std::copy(unordered_multimap2.begin(), unordered_multimap2.end(),std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));std::cout << std::endl;std::cout << std::endl;std::unordered_multimap<Cell, string, CHash, CEqual> unordered_multimap3;while (unordered_multimap3.size() < 5){unordered_multimap3.insert(generate());}std::cout << "unordered_multimap3:   ";std::copy(unordered_multimap3.begin(), unordered_multimap3.end(),std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));std::cout << std::endl;//2) 移动赋值运算符。用移动语义以 other 的内容替换内容(即从 other 移动 other 中的数据到此容器)。std::unordered_multimap<Cell, string, CHash, CEqual> unordered_multimap4 =std::move(unordered_multimap3);std::cout << "unordered_multimap4:   ";std::copy(unordered_multimap4.begin(), unordered_multimap4.end(),std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));std::cout << std::endl;std::cout << "unordered_multimap3 is empty " << unordered_multimap3.empty()<< "  bucket_count:   " << unordered_multimap3.bucket_count() << std::endl;std::cout << std::endl;//3) 以 initializer_list ilist 所标识者替换内容。std::unordered_multimap<Cell, string, CHash, CEqual> unordered_multimap5 ={generate(), generate(), generate(), generate(), generate(), generate()};std::cout << "unordered_multimap5:   ";std::copy(unordered_multimap5.begin(), unordered_multimap5.end(),std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));std::cout << std::endl;return 0;
}

输出

 

相关文章:

c++11 标准模板(STL)(std::unordered_multimap)(三)

定义于头文件 <unordered_map> template< class Key, class T, class Hash std::hash<Key>, class KeyEqual std::equal_to<Key>, class Allocator std::allocator< std::pair<const Key, T> > > class unordered…...

Linux进程控制-2

紧接着上篇博客出发&#xff0c;我们接着来讲述Linux中进程控制的内容。 目录 1.等待 1.1具体操作 1.等待 进程等待主要的作用在于&#xff1a;父进程创建子进程之后&#xff0c;等待子进程退出&#xff0c;获取子进程的退出码&#xff0c;释放子进程的资源&#xff0c;避…...

快速排序算法

一&#xff1a;快速排序思想 假设我们现在对“6 1 2 7 9 3 4 5 10 8”这个10个数进行排序。首先在这个序列中随便找一个数作为基准数&#xff08;不要被这个名词吓到了&#xff0c;就是一个用来参照的数&#xff0c;待会你就知道它用来做啥的了&#xff09;。为了方便&#xff…...

中华好诗词大学季第二季(四)

第七期 1,二十四友一朝尽&#xff0c;爱妾坠楼何足言出自许浑的《金谷园》&#xff0c;“爱妾”指的是谁 2,李白在《九月十日即事》借菊花表达自己的惋惜之情&#xff0c;请问九月十日是什么节日 A 后登高 B 菊花节 C 小重阳 3,贾宝玉在大观园里面题了“曲径通幽”&#xf…...

分布式系统容灾部署方案

本文主要以OceanBase部署来说明分布式系统容灾部署方案 分布式系统提供持续可用的服务尤为重要。 好的分布式系统根据需求提供不同等级的的高可用与容灾级别。 而在分布式系统中&#xff0c;数据库系统又是最核心最关键的系统。 我们以数据库分布式系统为主&#xff0c;考虑…...

Python 爬虫性能相关总结

这里我们通过请求网页例子来一步步理解爬虫性能 当我们有一个列表存放了一些url需要我们获取相关数据&#xff0c;我们首先想到的是循环 简单的循环串行 这一种方法相对来说是最慢的&#xff0c;因为一个一个循环&#xff0c;耗时是最长的&#xff0c;是所有的时间总和 代码…...

Baumer工业相机堡盟工业相机如何设置网口的IP地址(工业相机连接的网口设置IP地址步骤)

Baumer工业相机堡盟工业相机如何设置网口的IP地址&#xff08;工业相机连接的网口设置IP地址步骤&#xff09;Baumer工业相机Baumer工业相机设置网络端口IP地址匹配设置网络端口IP地址和工业相机IP地址匹配第一次打开CameraExplorer软件确认问题为IP地址不匹配问题打开网络连接…...

Android MediaCodec设置H264 Profile到High

H264 High Profile压缩率高&#xff0c;能降低码率&#xff0c;这里记录下MediaCodec Profile设置到High遇到的一些问题。 Android 4.1 就引入了MediaCodecInfo.CodecProfileLevel类&#xff0c;下面截取H264(AVC)的Profile和Level定义: /** Copyright (C) 2012 The Android O…...

QT之QSysInfo(查看电脑信息)

文章目录前言一、API使用总结前言 QSysInfo是Qt中用于获取有关运行应用程序的系统信息的类。 我们可以获取以下信息&#xff1a; 返回系统产品类型&#xff0c;如ios&#xff0c;windows&#xff0c;Linux等 返回当前系统的产品版本。 返回当前系统的内核类型。 返回当前系统的…...

中国塑料编织袋产业竞争状况及投资前景预测报告2023-2029年

中国塑料编织袋产业竞争状况及投资前景预测报告2023-2029年 KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK 《报告编号》: BG451639 《出版时间》: 2023年4月 《出版机构》: 中智正业研究院 免费售后 服务一年&#xff0c;具体内容及订购流程欢迎咨询客服人员 内容简介&…...

从头用脚分析FFmpeg源码 - av_read_frame

av_read_frame作用 /*** Return the next frame of a stream.* This function returns what is stored in the file, and does not validate* that what is there are valid frames for the decoder. It will split what is* stored in the file into frames and return one f…...

第17章_触发器

第17章_触发器 &#x1f3e0;个人主页&#xff1a;shark-Gao &#x1f9d1;个人简介&#xff1a;大家好&#xff0c;我是shark-Gao&#xff0c;一个想要与大家共同进步的男人&#x1f609;&#x1f609; &#x1f389;目前状况&#xff1a;23届毕业生&#xff0c;目前在某公…...

3956. 截断数组

3956. 截断数组 - AcWing题库 3956. 截断数组 【题目描述】 给定一个长度为 nn 的数组 a1,a2,…,ana1,a2,…,an。 现在&#xff0c;要将该数组从中间截断&#xff0c;得到三个非空子数组。 要求&#xff0c;三个子数组内各元素之和都相等。 请问&#xff0c;共有多少种不同…...

React Labs: 我们最近在做什么——2023 年 3 月

原文&#xff1a;https://react.dev/blog/2023/03/22/react-labs-what-we-have-been-working-on-march-2023 React Server Components React Server Components(下文简称 RSC) 是由 React 团队设计的新应用程序架构。 我们首先在一个介绍性演讲和一个RFC中分享了我们对 RSC 的…...

文件系统设计详解

抽象的文件系统以目录的形式来组织文件&#xff0c;我们可以利用该文件系统来读取某个文件的内容&#xff0c;也可以对目录或者文件实施监控并及时获取变化的通知。 IChangeToken IChangeToken对象就是一个与某组监控数据相关联的“令牌”&#xff08;Token&#xff09;&#x…...

好看~立马启动python实现美女通通下

人生苦短&#xff0c;我用python一、环境版本使用二、代码实现思路三、代码展示&#xff1a;导入模块伪装(请求头)四、部分好看截图&#xff0c;更多的就自己去采集噜~吃饭放松的时候哇一不小心看见了很多好看的东西 哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈 独乐乐不如众乐乐&#xf…...

Git 安装设置

1、安装 安装以下三个软件&#xff1a; Git-2.13.3-64-bit.exe TortoiseGit-2.4.0.2-64bit.msi TortoiseGit-LanguagePack-2.4.0.0-64bit-zh_CN.msi 安装过程中不用填写、不用选择&#xff0c;全部点"下一步"&#xff0c;完成后需要重启机器。 2、基本设…...

Python-闭包

介绍 Python的闭包是一种高级的编程技巧&#xff0c;它可以在函数内部定义另一个函数&#xff0c;并返回该函数的引用。这个内部函数可以访问外部函数的变量和参数&#xff0c;即使外部函数已经执行完毕 好处 1&#xff09;闭包可以避免全局变量的污染&#xff0c;使得代码更…...

Gitlab中Pipeline语法四

Gitlab中Pipeline语法 cache cache:paths 在job build中定义缓存,将会缓存target目录下的所有*.jar文件当全局定义了cache:paths会被job中覆盖.以下实例将缓存target目录 buld:script: buildcache:paths:- target/*.jar#设置key可以解决cache被覆盖 cache:paths:- my/files…...

Go语言精修(尚硅谷笔记)第五章

五、程序流程控制 5.1 单分支控制 package main import ("fmt" )func main() {//请大家看个案例[ifDemo.go]://编写一个程序,可以输入人的年龄,如果该同志的年龄大于18岁,则输出 "你年龄大//于18,要对自己的行为负责!"//分析 //1.年龄 > var age int…...

[特殊字符] 智能合约中的数据是如何在区块链中保持一致的?

&#x1f9e0; 智能合约中的数据是如何在区块链中保持一致的&#xff1f; 为什么所有区块链节点都能得出相同结果&#xff1f;合约调用这么复杂&#xff0c;状态真能保持一致吗&#xff1f;本篇带你从底层视角理解“状态一致性”的真相。 一、智能合约的数据存储在哪里&#xf…...

PPT|230页| 制造集团企业供应链端到端的数字化解决方案:从需求到结算的全链路业务闭环构建

制造业采购供应链管理是企业运营的核心环节&#xff0c;供应链协同管理在供应链上下游企业之间建立紧密的合作关系&#xff0c;通过信息共享、资源整合、业务协同等方式&#xff0c;实现供应链的全面管理和优化&#xff0c;提高供应链的效率和透明度&#xff0c;降低供应链的成…...

FastAPI 教程:从入门到实践

FastAPI 是一个现代、快速&#xff08;高性能&#xff09;的 Web 框架&#xff0c;用于构建 API&#xff0c;支持 Python 3.6。它基于标准 Python 类型提示&#xff0c;易于学习且功能强大。以下是一个完整的 FastAPI 入门教程&#xff0c;涵盖从环境搭建到创建并运行一个简单的…...

【Redis技术进阶之路】「原理分析系列开篇」分析客户端和服务端网络诵信交互实现(服务端执行命令请求的过程 - 初始化服务器)

服务端执行命令请求的过程 【专栏简介】【技术大纲】【专栏目标】【目标人群】1. Redis爱好者与社区成员2. 后端开发和系统架构师3. 计算机专业的本科生及研究生 初始化服务器1. 初始化服务器状态结构初始化RedisServer变量 2. 加载相关系统配置和用户配置参数定制化配置参数案…...

全球首个30米分辨率湿地数据集(2000—2022)

数据简介 今天我们分享的数据是全球30米分辨率湿地数据集&#xff0c;包含8种湿地亚类&#xff0c;该数据以0.5X0.5的瓦片存储&#xff0c;我们整理了所有属于中国的瓦片名称与其对应省份&#xff0c;方便大家研究使用。 该数据集作为全球首个30米分辨率、覆盖2000–2022年时间…...

最新SpringBoot+SpringCloud+Nacos微服务框架分享

文章目录 前言一、服务规划二、架构核心1.cloud的pom2.gateway的异常handler3.gateway的filter4、admin的pom5、admin的登录核心 三、code-helper分享总结 前言 最近有个活蛮赶的&#xff0c;根据Excel列的需求预估的工时直接打骨折&#xff0c;不要问我为什么&#xff0c;主要…...

1.3 VSCode安装与环境配置

进入网址Visual Studio Code - Code Editing. Redefined下载.deb文件&#xff0c;然后打开终端&#xff0c;进入下载文件夹&#xff0c;键入命令 sudo dpkg -i code_1.100.3-1748872405_amd64.deb 在终端键入命令code即启动vscode 需要安装插件列表 1.Chinese简化 2.ros …...

SpringBoot+uniapp 的 Champion 俱乐部微信小程序设计与实现,论文初版实现

摘要 本论文旨在设计并实现基于 SpringBoot 和 uniapp 的 Champion 俱乐部微信小程序&#xff0c;以满足俱乐部线上活动推广、会员管理、社交互动等需求。通过 SpringBoot 搭建后端服务&#xff0c;提供稳定高效的数据处理与业务逻辑支持&#xff1b;利用 uniapp 实现跨平台前…...

Python如何给视频添加音频和字幕

在Python中&#xff0c;给视频添加音频和字幕可以使用电影文件处理库MoviePy和字幕处理库Subtitles。下面将详细介绍如何使用这些库来实现视频的音频和字幕添加&#xff0c;包括必要的代码示例和详细解释。 环境准备 在开始之前&#xff0c;需要安装以下Python库&#xff1a;…...

大模型多显卡多服务器并行计算方法与实践指南

一、分布式训练概述 大规模语言模型的训练通常需要分布式计算技术,以解决单机资源不足的问题。分布式训练主要分为两种模式: 数据并行:将数据分片到不同设备,每个设备拥有完整的模型副本 模型并行:将模型分割到不同设备,每个设备处理部分模型计算 现代大模型训练通常结合…...