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;
}

输出

 

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

2023年的深度学习入门指南(2) - 给openai API写前端

2023年的深度学习入门指南(2) - 给openai API写前端 上一篇我们说了&#xff0c;目前的大规模预训练模型技术还避免不了回答问题时出现低级错误。 但是其实&#xff0c;人类犯的逻辑错误也是层出不穷。 比如&#xff0c;有人就认为要想学好chatgpt&#xff0c;就要先学好Pyth…

【AI JUST AI】Stable Disffusion | 配合Chrome插件,与Notion API完美联动

【AI JUST AI】Stable Disffusion | 配合Chrome插件&#xff0c;与Notion API完美联动第一步、Stable Diffusion 链接 CMS开发Chrome插件在合适的位置增加一个发送至Notion的按钮编写按钮的逻辑部分使用GitHub作为图床图片上传 API第二步&#xff0c;使用Chat GPT优化样式Stabl…

超详细WindowsJDK1.8与JDK11版本切换教程

文章目录一、JDK生效原理二、安装配置JDK11三、切换JDK11版本四、查看切换JDK11版本是否成功五、再次切换至JDK8版本六、查看切换JDK8版本是否成功一、JDK生效原理 想必大家都在为如何流畅的切换JDK版本问题而来&#xff0c;那么在此篇文章开始之前&#xff0c;首先我们来思考一…

网络传输层

目录传输层再谈端口号端口号范围划分认识知名端口号netstatpidofUDP协议UDP协议端格式UDP的特点面向数据报UDP的缓冲区UDP使用注意事项使用udp协议 的应用层协议其它TCP协议TCP协议段格式如何理解链接如何理解三次握手如何理解四次挥手概念TIME_WAIT/CLOSE_WAITTCP策略确认应答…

【wps】【毕业论文】三线表的绘制

目录 一、三线表 二、制作步骤 &#xff08;1&#xff09;点击“插入”——点击“表格”创建一个表格 &#xff08;2&#xff09;选中整个表格——鼠标右键选择“边框和底纹”&#xff0c;“表格属性”再点击“边框和底纹”——点击“自定义”——选择表格的边的宽度——如图…

北京筑龙智能寻源 |助力企业一站式智能采购,降本增效

智能寻源——精准匹配&#xff0c;让采购更高效 智能寻源系统是北京筑龙为采购人搭建的一款全链路高效协同的采购寻源和供应商管理平台。助力采购人快速完成采购计划&#xff0c;提升采购效率&#xff0c;降低采购风险。 基于智能寻源系统&#xff0c;将全面打通供应商数据壁…

VR数字政务,VR全景技术,探索数字化治理新路径

近年来&#xff0c;随着虚拟现实&#xff08;VR&#xff09;技术的不断发展&#xff0c;VR数字政务也逐渐成为行政数字化转型的重要组成部分。VR数字政务可以为行政部门提供全新的数字化解决方案&#xff0c;使行政部门的工作更加高效、便捷和安全。 一、VR数字政务的定义和概述…

ABBYY FineReader PDF15下载安装教程

刚刚&#xff0c;老板给我一堆扫描文件&#xff08;图片和pdf文件&#xff09;&#xff0c;拿不到源文件&#xff0c;让我把客户发的扫描文件搞成word文档&#xff0c;密密麻麻&#xff0c;这些文件100多页&#xff0c;这要手工敲能把手敲费。 这时候&#xff0c;让我想到了这…

小白的git入门教程(三)

书接上文&#xff0c;我们讲到如何进行版本日志回退&#xff0c;根据这个&#xff0c;我们可以返回到任意状态 今天让我们接着讲完git的基本指令操作教程以及其余分支 删除文件操作 前提&#xff1a;要被删除的文件已经存储在本地库中 这里我们可以创建一个文件&#xff08;待…

ActiViz.NET 9.2.2023 Crack

适用于 .Net C# 和 Unity 的 3D 可视化库 释放可视化工具包的强大功能&#xff0c;在 C#、.Net 和 Unity 软件中为您的 3D 内容服务。 ActiViz 允许您轻松地将 3D 可视化集成到您的应用程序中。 Kitware 围绕 ActiViz 和 3D 应用程序提供支持和自定义开发 活动可视化功能 C…

【Java代码审计】表达式注入

1 前置知识 1.1 EL表达式 EL表达式主要功能&#xff1a; 获取数据&#xff1a;可以从JSP四大作用域中获取数据执行运算&#xff1a;执行一些关系运算&#xff0c;逻辑运算&#xff0c;算术运算获取web开发常用对象&#xff1a;通过内置 的11个隐式对象获取想要的数据调用jav…

STL容器之initializer_list与set

STL容器之initializer_list与setinitializer_list案例二&#xff08;实现n个数的加法&#xff09;set单集合有序性唯一性删除元素多重集合less与greater自定义类型initializer_list initializer_list创建的对象&#xff0c;初始值可以有很多个&#xff0c;像vector 一样 想多少…

第05章_排序与分页

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

SPI、I2C、CAN通信的简单介绍和笔记

标题中的三种通信方式&#xff08;协议&#xff09;是比较常见的一些通信协议&#xff0c;对于它们有一定的了解对于我们学习嵌入式单片机的学习有着非常重要的作用。于是我们对此有一些信息给到各位读者&#xff0c;这也是笔者自己巩固知识点的方式。如果觉得有帮到各位&#…

551、Elasticsearch详细入门教程系列 -【分布式全文搜索引擎 Elasticsearch(二)】 2023.04.04

目录一、Elasticsearch创建/查看/删除索引、创建/查看/修改/删除文档、映射关系1.1 Elasticsearch中的数据格式1.2 索引操作1.2.1 创建索引1.2.2 查看指定索引1.2.3 查看全部索引1.2.4 删除索引1.3 文档操作1.3.1 创建文档1.3.2 查看单个文档&#xff1a;主键查询1.3.3 查看所有…

不敲代码用ChatGPT开发一个App

先说下背景&#xff0c;有一天我在想 ChatGPT 对于成熟的开发者来说已经是一个非常靠谱的助手了&#xff0c;身边也确实有很多同事把它作为一个离不开的助理担当。 但是如果我只是略微懂一点前端知识的新人&#xff0c;了解 HTML、CSS、JS 相关的知识&#xff0c;想开发一个安…

什么是UEFI签名认证?UEFI签名有什么好处?

为了防御恶意软件攻击&#xff0c;目前市面上所有电脑设备启动时默认开启安全启动(Secure Boot)模式。安全启动(Secure Boot)是UEFI扩展协议定义的安全标准&#xff0c;可以确保设备只使用OEM厂商信任的软件启动。UEFI签名认证就是对运行在 UEFI 系统下的 efi 驱动和通过 UEFI …

第10章_创建和管理表

第10章_创建和管理表 &#x1f3e0;个人主页&#xff1a;shark-Gao &#x1f9d1;个人简介&#xff1a;大家好&#xff0c;我是shark-Gao&#xff0c;一个想要与大家共同进步的男人&#x1f609;&#x1f609; &#x1f389;目前状况&#xff1a;23届毕业生&#xff0c;目前…

OpenCloudOS 9.0发布,腾讯闯入底层基础软件“深水区”

3月22日&#xff0c;腾讯发布了2022第四季度及全年业绩&#xff0c;ToB业务成为腾讯的核心引擎。与此同时&#xff0c;ToB的腾讯在近年来持续加码自研投入&#xff0c;提升底层技术实力&#xff0c;2022年研发投入达到614亿元&#xff0c;2018年至今在研发上的投入已经超过2056…

Mockito单测之道

Mockito单测之道 去年写过一篇《TestNG单元测试实战》文章&#xff0c;严格来讲算集成测试。 没看的小伙伴可直接看本篇即可&#xff0c;本质是单元测试框架不同&#xff0c;写法不一样。 单测定义 单元测试定义&#xff1a; 对软件中最小可测单元进行验证&#xff0c;可理解…
最新文章