c++11 标准模板(STL)(std::unordered_map)(六)

news2024/10/1 17:33:26
定义于头文件 <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_map;
(1)(C++11 起)
namespace pmr {

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

}
(2)(C++17 起)

 

修改器

插入元素或结点

std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::insert

std::pair<iterator,bool> insert( const value_type& value );

(1)(C++11 起)

std::pair<iterator,bool> insert( value_type&& value );

(1)(C++17 起)

template< class P >
std::pair<iterator,bool> insert( P&& value );

(2)(C++11 起)

iterator insert( const_iterator hint, const value_type& value );

(3)(C++11 起)

iterator insert( const_iterator hint, value_type&& value );

(3)(C++17 起)

template< class P >
iterator insert( const_iterator hint, P&& value );

(4)(C++11 起)

template< class InputIt >
void insert( InputIt first, InputIt last );

(5)(C++11 起)

void insert( std::initializer_list<value_type> ilist );

(6)(C++11 起)

insert_return_type insert(node_type&& nh);

(7)(C++17 起)

iterator insert(const_iterator hint, node_type&& nh);

(8)(C++17 起)

 

若容器尚未含有带等价关键的元素,则插入元素到容器中。

1-2) 插入 value 。重载 (2) 等价于 emplace(std::forward<P>(value)) ,且仅若 std::is_constructible<value_type, P&&>::value == true 才参与重载决议。

3-4) 插入 value ,以 hint 为应当开始搜索的位置的非强制建议。重载 (4) 等价于 emplace_hint(hint, std::forward<P>(value)) ,且仅若 std::is_constructible<value_type, P&&>::value == true 才参与重载决议。

5) 插入来自范围 [first, last) 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的(待决的 LWG2844 )。

6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的(待决的 LWG2844 )。

7) 若 nh 是空的结点把柄,则不做任何事。否则插入 nh 所占有的元素到容器,若容器尚未含有拥有等价于 nh.key() 的关键的元素。若 nh 非空且 get_allocator() != nh.get_allocator() 则行为未定义。

8) 若 nh 是空的结点把柄,则不做任何事并返回尾迭代器。否则,插入 nh 所占有的元素到容器,若容器尚未含有拥有等价于 nh.key() 的关键的元素,并返回指向拥有等于 nh.key() 的关键的元素的迭代器(无关乎插入成功还是失败)。若插入成功,则从 nh 移动,否则它保持该元素的所有权。元素被插入到尽可能接近 hint 的位置。若 nh 非空且 get_allocator() != nh.get_allocator() 则行为未定义。

若因插入发生重哈希,则所有迭代器都被非法化。否则迭代器不受影响。引用不受影响。重哈希仅若新元素数量大于 max_load_factor()*bucket_count() 才发生。若插入成功,则在结点把柄保有元素时获得的指向该元素的指针和引用被非法化,而在提取前获得的指向元素的指针和引用变得合法。 (C++17 起)

参数

hint-迭代器,用作插入内容位置的建议
value-要插入的元素值
first, last-要插入的元素范围
ilist-插入值来源的 initializer_list
nh-兼容的结点把柄
类型要求
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。

返回值

1-2) 返回由指向被插入元素(或阻止插入的元素)的迭代器和指代插入是否发生的 bool 组成的 pair 。

3-4) 返回指向被插入元素,或阻止插入的元素的迭代器。

5-6) (无)

7) 返回 insert_return_type ,其成员初始化如下:若 nh 为空,则 insertedfalseposition 为 end() ,而 node 为空。否则发生插入, insertedtrueposition 指向被插入元素,而 node 为空。若插入失败,则 insertedfalsenode 拥有 nh 的先前值,而 position 指向拥有等价于 nh.key() 的关键的元素。

8) 若 nh 为空则为尾迭代器,若插入发生则为指向被插入元素的迭代器,而若插入失败则为指向拥有等价于 nh.key() 的关键的元素的迭代器。

异常

1-4) 若任何操作抛出异常,则插入无效果。

本节未完成
原因:情况 5-6

复杂度

1-4) 平均情况: O(1) ,最坏情况 O(size())

5-6) 平均情况: O(N) ,其中 N 是要插入的元素数。最坏情况: O(N*size()+N)

7-8) 平均情况: O(1) ,最坏情况 O(size())

注意

有提示插入 (3,4) 不返回 bool ,这是为了与顺序容器上的定位插入,如 std::vector::insert 签名兼容。这使得可以创建泛型插入器,例如 std::inserter 。检查有提示插入是否成功的一种方式是比较插入前后的 size() 。

调用示例

#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()
{
    std::cout << std::boolalpha;

    std::mt19937 g{std::random_device{}()};
    srand((unsigned)time(NULL));

    auto generate = []()
    {
        int n = std::rand() % 10 + 110;
        Cell cell{n, n};
        return std::pair<Cell, string>(cell, std::to_string(n));
    };

    std::unordered_map<Cell, string, CHash, CEqual> unordered_map1;
    for (size_t index = 0; index < 5; index++)
    {
        //若容器尚未含有带等价关键的元素,则插入元素到容器中。1-2) 插入 value 。
        std::pair<std::unordered_map<Cell, string, CHash, CEqual>::iterator, bool> iit
            = unordered_map1.insert(generate());
        std::cout << "unordered_map1 insert : " << *iit.first << "   " << iit.second << std::endl;
    }
    std::cout << "unordered_map1:   ";
    std::copy(unordered_map1.begin(), unordered_map1.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::unordered_map<Cell, string, CHash, CEqual> unordered_map2;
    for (size_t index = 0; index < 5; index++)
    {
        std::pair<Cell, string> cell = generate();
        //若容器尚未含有带等价关键的元素,则插入元素到容器中。1-2) 插入 value 。移动语义
        std::pair<std::unordered_map<Cell, string, CHash, CEqual>::iterator, bool> iit
            = unordered_map2.insert(std::move(cell));
        std::cout << "unordered_map2 insert : " << *iit.first << "   " << iit.second << std::endl;
    }
    std::cout << "unordered_map2:   ";
    std::copy(unordered_map2.begin(), unordered_map2.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::unordered_map<Cell, string, CHash, CEqual> unordered_map3;
    for (size_t index = 0; index < 5; index++)
    {
        //3-4) 插入 value ,以 hint 为应当开始搜索的位置的非强制建议。
        std::unordered_map<Cell, string, CHash, CEqual>::iterator iit
            = unordered_map3.insert(unordered_map3.cbegin(), generate());
        std::cout << "unordered_map3 insert : " << *iit  << std::endl;
    }
    std::cout << "unordered_map3:   ";
    std::copy(unordered_map3.begin(), unordered_map3.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::unordered_map<Cell, string, CHash, CEqual> unordered_map4;
    for (size_t index = 0; index < 5; index++)
    {
        std::pair<Cell, string> cell = generate();
        //3-4) 插入 value ,以 hint 为应当开始搜索的位置的非强制建议。移动语义
        std::unordered_map<Cell, string, CHash, CEqual>::iterator iit
            = unordered_map4.insert(unordered_map4.cbegin(), std::move(cell));
        std::cout << "unordered_map4 insert : " << *iit  << std::endl;
    }
    std::cout << "unordered_map4:   ";
    std::copy(unordered_map4.begin(), unordered_map4.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::vector<std::pair<Cell, string>> vector1(6);
    std::generate(vector1.begin(), vector1.end(), generate);
    std::cout << "vector1:          ";
    std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::unordered_map<Cell, string, CHash, CEqual> unordered_map5;
    //5) 插入来自范围 [first, last) 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
    unordered_map5.insert(vector1.begin(), vector1.end());
    std::cout << "unordered_map5:   ";
    std::copy(unordered_map5.begin(), unordered_map5.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::unordered_map<Cell, string, CHash, CEqual> unordered_map6;
    //6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
    unordered_map6.insert({generate(), generate(), generate(), generate(), generate(), generate()});
    std::cout << "unordered_map6:   ";
    std::copy(unordered_map6.begin(), unordered_map4.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;

    return 0;
}

输出 

 

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Transformers】IMDB 分类

安装 transformers 库。 !pip install transformersimport numpy as np import pandas as pd import tensorflow as tf import tensorflow_datasets as tfdsfrom transformers import BertTokenizer from sklearn.model_selection import train_test_split from transformers i…

IO学习、拓展贴

1. 字节流 1.1 FileInputStream import org.junit.jupiter.api.Test;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;/*** 演示FileInputStream的使用(字节输入流 文件--> 程序)*/ public class FileInputStream_ {pu…

10款最佳项目管理工具推荐,总有一款适合你

为什么需要项目管理工具&#xff1f; 如今企业规模不断扩大&#xff0c;业务逐渐复杂化&#xff0c;项目管理已经成为现代企业管理中不可或缺的一环&#xff1b; 如果没有合适的项目管理工具&#xff0c;我们的项目管理和跟踪就会变得非常困难。这可能导致项目延期或者出现一…

免费Api接口汇总(亲测可用,可写项目)

免费Api接口汇总&#xff08;亲测可用&#xff09;1. 聚合数据2. 用友API3. 天行数据4. Free Api5. 购物商城6. 网易云音乐API7. 疫情API8. 免费Api合集1. 聚合数据 https://www.juhe.cn/ 2. 用友API http://iwenwiki.com/wapicovid19/ 3. 天行数据 https://www.tianapi.com…

RK356x U-Boot研究所(命令篇)3.9 scsi命令的用法

平台U-Boot 版本Linux SDK 版本RK356x2017.09v1.2.3文章目录 一、设备树与config配置二、scsi命令的定义三、scsi命令的用法3.1 scsi总线扫描3.2 scsi设备读写一、设备树与config配置 RK3568支持SATA接口,例如ROC-RK3568-PC: 原理图如下: 可以新建一个rk3568-sata.config配…

Oracle listagg,wm_concat函数行转列结果去重Oracle 11g/19c版本

1、准备数据表 2、根据学生名(stu_name)分组&#xff0c;学生名相同的&#xff0c;学生年龄(stu_age)用逗号拼接&#xff0c;使用 listagg&#xff08;&#xff09;函数法拼接 3、上图中出现了两个12,12&#xff0c;实现去重 3.1 listagg&#xff08;&#xff09; 函数 去重 【…

网络协议(十一):单向散列函数、对称加密、非对称加密、混合密码系统、数字签名、证书

网络协议系列文章 网络协议(一)&#xff1a;基本概念、计算机之间的连接方式 网络协议(二)&#xff1a;MAC地址、IP地址、子网掩码、子网和超网 网络协议(三)&#xff1a;路由器原理及数据包传输过程 网络协议(四)&#xff1a;网络分类、ISP、上网方式、公网私网、NAT 网络…

怎么把tif格式转成jpg?快速无损转换

怎么把tif格式转成jpg&#xff1f;在编辑使用图片的时候&#xff0c;弄清各种图片格式的特点是很重要的&#xff0c;因为图片总因自身格式具备的特点不同常常出现打不开的情况&#xff0c;或者占的体积大&#xff0c;这都会直接影响我们的使用。所以目前很多的图片格式都需要提…

spring boot整合RabbitMQ

文章目录 目录 文章目录 前言 一、环境准备 二、使用步骤 2.1 RabbitMQ高级特性 2.1.1 消息的可靠性传递 2.1.2 Consumer Ack 2.2.3 TTL 2.2.4 死信队列 总结 前言 一、环境准备 引入依赖生产者和消费都引入这个依赖 <dependency><groupId>org.springframework…

自动化测试总结--断言

采购对账测试业务流程中&#xff0c;其中一个测试步骤总是失败&#xff0c;原因是用例中参数写错及断言不明确 一、问题现象&#xff1a; 采购对账主流程中&#xff0c;其中一个步骤失败了&#xff0c;会导致这个套件一直失败 图&#xff08;1&#xff09;测试报告视图中&…

Navicate远程连接Linux上docker安装的MySQL容器

Navicate远程连接Linux上docker安装的MySQL容器失败 来自&#xff1a;https://bluebeastmight.github.io/ 问题描述&#xff1a;windows端的navicat远程连接不上Linux上docker安装的mysql&#xff08;5.7版本&#xff09;容器&#xff0c;错误代码10060 标注&#xff1a; 1、…

XSS攻击防御

XSS攻击防御XSS Filter过滤方法输入验证数据净化输出编码过滤方法Web安全编码规范XSS Filter XSS Filter的作用是通过正则的方式对用户&#xff08;客户端&#xff09;请求的参数做脚本的过滤&#xff0c;从而达到防范XSS攻击的效果。 XSS Filter作为防御跨站攻击的主要手段之…

C++ Primer阅读笔记--书包程序

1--该章节新知识点 ① 在 UNIX 和 Windows 系统中&#xff0c;执行完一个程序后&#xff0c;可以通过 echo 命令获得其返回值&#xff1b; # UNIX系统中&#xff0c;通过如下命令获得状态 echo $? ② 在标准库中&#xff0c;定义了两个输出流 ostream 对象&#xff1a;cerr…

运维效率狂飙,都在告警管理上

随着数字化进程的加速&#xff0c;企业IT设备和系统越来越多&#xff0c;告警和流程中断风险也随之增加。每套系统和工具发出的警报&#xff0c;听起来像是一场喧嚣的聚会&#xff0c;各自谈论不同的话题。更糟糕的是&#xff0c;安全和运维团队正在逐渐丧失对告警的敏感度&…

2.Fully Convolutional Networks for Semantic Segmentation论文记录

欢迎访问个人网络日志&#x1f339;&#x1f339;知行空间&#x1f339;&#x1f339; 文章目录1.基础介绍2.分类网络转换成全卷积分割网络3.转置卷积进行上采样4.特征融合5.一个pytorch源码实现参考资料1.基础介绍 论文:Fully Convolutional Networks for Semantic Segmentati…

如何用postman实现接口自动化测试

postman使用 开发中经常用postman来测试接口&#xff0c;一个简单的注册接口用postman测试&#xff1a; 接口正常工作只是最基本的要求&#xff0c;经常要评估接口性能&#xff0c;进行压力测试。 postman进行简单压力测试 下面是压测数据源&#xff0c;支持json和csv两个格…

Java反序列化漏洞——jdbc反序列化漏洞利用

漏洞原理如果攻击者能够控制JDBC连接设置项&#xff0c;那么就可以通过设置其指向恶意MySQL服务器进行ObjectInputStream.readObject()的反序列化攻击从而RCE。具体点说&#xff0c;就是通过JDBC连接MySQL服务端时&#xff0c;会有几个内置的SQL查询语句要执行&#xff0c;其中…

汽车用CAN通讯接口简介

随着新能源的普及,汽车用的芯片数量也越来越多,汽车在进行新四化(电动化、网联化、智能化、共享化),Gateway整车控制中心、TBox网联设备、IVI智能座舱、智驾域控制器等等ECU变得更智能,车控指令和车内通信变得更加丰富。车内ECU通讯比如CAN、LIN、蓝牙还有人提出高速以太…

pyflink学习笔记(四):datastream_api

现pyflink环境为1.16 &#xff0c;下面介绍下常用的datastream算子。现我整理的都是简单的、常用的&#xff0c;后期会继续补充。官网&#xff1a;https://nightlies.apache.org/flink/flink-docs-release-1.16/docs/dev/python/datastream/intro_to_datastream_api/from pyfli…

面向新时代,海泰方圆战略升级!“1465”隆重发布!

过去四年&#xff0c;海泰方圆“1344”战略一直在引领公司前行&#xff0c;搭建了非常坚实的战略框架基座&#xff0c;并推动全员在实践和行动中达成深度共识。 “1344”战略 1个定位&#xff0c;代表着当前机构用户的一组共性需求&#xff0c;密码安全数据治理信创工程。 3…