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

news2024/10/3 16:31:10
定义于头文件 <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 起)

 unordered_map 是关联容器,含有带唯一键的键-值 pair 。搜索、插入和元素移除拥有平均常数时间复杂度。

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

 

容量

检查容器是否为空

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

bool empty() const noexcept;

(C++11 起)
(C++20 前)

[[nodiscard]] bool empty() const noexcept;

(C++20 起)

 检查容器是否无元素,即是否 begin() == end() 。

参数

(无)

返回值

若容器为空则为 true ,否则为 false

复杂度

常数。

 

返回容纳的元素数

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

size_type size() const noexcept;

(C++11 起)

参数

(无)

返回值

容器中的元素数量。

复杂度

常数。

 

返回可容纳的最大元素数

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

size_type max_size() const noexcept;

(C++11 起)

返回根据系统或库实现限制的容器可保有的元素最大数量,即对于最大容器的 std::distance(begin(), end()) 。

参数

(无)

返回值

元素数量的最大值。

复杂度

常数。

注意

此值通常反映容器大小上的理论极限,至多为 std::numeric_limits<difference_type>::max() 。运行时,可用 RAM 总量可能会限制容器大小到小于 max_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::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;

    //检查容器是否无元素,即是否 begin() == end() 。
    std::unordered_map<Cell, string, CHash, CEqual> unordered_map1;
    std::cout << "unordered_map1 is empty " << unordered_map1.empty() << std::endl;

    unordered_map1 = {generate(), generate(), generate(), generate(), generate(), generate()};;
    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 << "unordered_map1 is empty " << unordered_map1.empty() << std::endl;
    std::cout << std::endl;

    std::unordered_map<Cell, string, CHash, CEqual> unordered_map2;
    for (size_t index = 0; index < 6; index++)
    {
        unordered_map2.insert(generate());
        //返回容器中的元素数,即 std::distance(begin(), end()) 。
        std::cout << "unordered_map2 size:  " << unordered_map2.size() << std::endl;
        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::distance(begin(), end()) 。
    std::unordered_map<char, char> unordered_map_c;
    std::cout << "unordered_map<char, char> max_size:           " << unordered_map_c.max_size() << std::endl;
    std::unordered_map<int, int> unordered_map_i;
    std::cout << "unordered_map<int, int> max_size:             " << unordered_map_i.max_size() << std::endl;
    std::unordered_map<uint8_t, uint8_t> unordered_map_ui8;
    std::cout << "unordered_map<uint8_t, uint8_t> max_size:     " << unordered_map_ui8.max_size() << std::endl;
    std::unordered_map<uint16_t, uint16_t> unordered_map_ui16;
    std::cout << "unordered_map<uint16_t, uint16_t> max_size:   " << unordered_map_ui16.max_size() << std::endl;
    std::unordered_map<uint32_t, uint32_t> unordered_map_ui32;
    std::cout << "unordered_map<uint32_t,uint32_t> max_size:    " << unordered_map_ui32.max_size() << std::endl;
    std::unordered_map<uint64_t, uint64_t> unordered_map_ui64;
    std::cout << "unordered_map<uint64_t, uint64_t> max_size:   " << unordered_map_ui64.max_size() << std::endl;
    std::unordered_map<short, short> unordered_map_s;
    std::cout << "unordered_map<short, short> max_size:         " << unordered_map_s.max_size() << std::endl;
    std::unordered_map<double, double> unordered_map_d;
    std::cout << "unordered_map<double, double> max_size:       " << unordered_map_d.max_size() << std::endl;
    std::unordered_map<float, float> unordered_map_f;
    std::cout << "unordered_map<float, float> max_size:         " << unordered_map_f.max_size() << std::endl;
    std::unordered_map<long, long> unordered_map_l;
    std::cout << "unordered_map<long, long> max_size:           " << unordered_map_l.max_size() << std::endl;
    std::unordered_map<long long, long long> unordered_map_ll;
    std::cout << "unordered_map<long long, long long> max_size: " << unordered_map_ll.max_size() << std::endl;
    std::unordered_map<string, string> unordered_map_str;
    std::cout << "unordered_map<string, string> max_size:       " << unordered_map_str.max_size() << std::endl;
    std::unordered_map<Cell, Cell, CHash, CEqual> unordered_map_Cell;
    std::cout << "unordered_map<Cell, Cell> max_size:           " << unordered_map_Cell.max_size() << std::endl;

    return 0;
}

输出

 

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

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

相关文章

Java开发 - 单点登录初体验(Spring Security + JWT)

目录​​​​​​​ 前言 为什么要登录 登录的种类 Cookie-Session Cookie-Session-local storage JWT令牌 几种登陆总结 用户身份认证与授权 创建工程 添加依赖 启动项目 Bcrypt算法的工具 创建VO模型类 创建接口文件 创建XML文件 补充配置 添加依赖 添加配…

凭一部手机,7天赚20万?会剪辑的人有多吃香!

影视剪辑容易遇到哪些问题&#xff1a; 1、视频格式格式不对&#xff0c;剪辑软件不支持&#xff1b; 2、视频封面不会做&#xff1b; 3、PR导出视频时&#xff0c;没办法做其他事&#xff0c;效率不高&#xff1b; 4、自己配音不好听&#xff0c;配音软件又不好找&#xff1b;…

第14章 局部波动率模型

这学期会时不时更新一下伊曼纽尔德曼&#xff08;Emanuel Derman&#xff09; 教授与迈克尔B.米勒&#xff08;Michael B. Miller&#xff09;的《The Volatility Smile》这本书&#xff0c;本意是协助导师课程需要&#xff0c;发在这里有意的朋友们可以学习一下&#xff0c;思…

影响redis性能的一些潜在因素

影响 Redis 性能的 5 大方面的潜在因素&#xff0c;分别是&#xff1a; Redis 内部的阻塞式操作&#xff1b; CPU 核和 NUMA 架构的影响&#xff1b; Redis 关键系统配置&#xff1b; Redis 内存碎片&#xff1b; Redis 缓冲区。 先学习了解下 Redis 内部的阻塞式操作以及应对的…

【数据架构系列-03】数据仓库、大数据平台、数据中台... 我不太认同《DataFun数据智能知识地图》中的定义

关注DataFunTalk有2年多了&#xff0c;DataFun确实像创始人王大川讲的那样&#xff0c;践行选择、努力和利他原则&#xff0c;专注于大数据、人工智能技术应用的分享与交流&#xff0c;秉承着开源开放的精神&#xff0c;免费的共享了很多有营养的行业实践专业知识&#xff0c;对…

1.win10环境搭建Elasticsearch7.2.0环境

环境介绍jdk1.8安装Elasticsearch7.2.0下载安装包直接解压进入到bin目录&#xff0c;双击elasticsearch.bates启动成功访问http://localhost:9200/jdk版本1.8,很有可能因为jdk版本的问题es启动失败支持连接https://www.elastic.co/cn/support/matrix#matrix_jvm安装Kibana7.2.0…

云计算介绍,让你更了解云计算

同学们好&#xff01; 第一次接触IT行业吗&#xff1f;没关系&#xff0c;看完这篇文章肯定会让你不再陌生。给自己几分钟时间&#xff0c;认真看完哦&#xff01; 1、不知道什么是云计算&#xff1f; 网络计算云计算 官方定义是&#xff1a;通过网络提供可伸缩的分布式计算…

建立相关在线社群的3个简单步骤

在线社群管理和社交媒体营销通常被视为一回事。虽然社群管理确实是社交媒体营销的一个关键部分&#xff0c;但它的意义超越了社交媒体的内容发布。因此&#xff0c;在线社群对于企业的数字营销十分重要。创建、维护和发展社群不是一件容易的工作&#xff0c;也不是一个快速的过…

枚举学习贴

1. 概述 1.1 是什么 枚举对应英文(enumeration, 简写 enum)枚举是一组常量的集合。可以这里理解&#xff1a;枚举属于一种特殊的类&#xff0c;里面只包含一组有限的特定的对象 1.2 枚举的二种实现方式 自定义类实现枚举使用 enum 关键字实现枚举 1.3 什么时候用 存在有限…

利用HGT聚类单细胞多组学数据并推理生物网络

单细胞多组学数据允许同时对多种组学数据进行定量分析&#xff0c;以捕捉复杂的分子机制和细胞异质性。然而现有的工具不能有效地推断不同细胞类型的活性生物网络以及这些网络对外部刺激的反应。 来自&#xff1a;Single-cell biological network inference using a heterogen…

操作系统_Linux_问答_2023_自用

GeeksforGeeks&#xff08;https://www.geeksforgeeks.org/&#xff09;&#xff1a;GeeksforGeeks是一个技术学习平台&#xff0c;它提供了广泛的操作系统知识&#xff0c;包括操作系统概念、进程管理、内存管理、文件系统等内容。IBM Developer&#xff08;https://developer…

代理模式-大话设计模式

一、定义 代理模式的定义&#xff1a;为其他对象提供一种代理以控制对这个对象的访问。在某些情况下&#xff0c;一个对象不适合或者不能直接引用另一个对象&#xff0c;而代理对象可以在客户端和目标对象之间起到中介的作用。 著名的代理模式例子为引用计数&#xff08;英语…

如何基于AI智能视频技术实现公园景区的人流量实时统计?

一、方案背景春暖花开的季节来临&#xff0c;外出旅游的人群也越来越多。无论是景区、公园、博物馆、步行街等场所&#xff0c;客流超载非常大&#xff0c;给游客带来的体验较差&#xff0c;同时也存在安全隐患。当前景区面临的管理痛点包括&#xff1a;客流信息查询难&#xf…

Hadoop3.1.3单机(伪分布式配置)

参考&#xff1a;林子雨老师网站博客 Hadoop安装搭建伪分布式教程&#xff08;全面&#xff09;吐血整理 环境 Vmare12 Ubuntu16.04 创建Hadoop用户 若安装Ubuntu不是用的“hadoop”用户&#xff0c;则需要增加一个名为"hadoop"的用户 直接快捷键ctrlaltt或者点…

【C语言督学训练营 第二天】C语言中的数据类型及标准输入输出

文章目录一、前言二、数据类型1.基本数据类型①.整形②.浮点型③.字符型2.高级数据类型3.数据分类①.常量②.变量三、标准输入输出1.scanf2.printf四、进制转换1.进制转换简介2.十进制转其他进制3.其他进制转换五、OJ网站的使用一、前言 王道2024考研408C语言督学营第二天&…

公安室内射击场设计

公安室内射击场是为了训练和提高警察、特警、部队等职业人士的射击技能而设计的。其设计需要考虑的因素包括安全性、实用性、灵活性、耐久性等多个方面。下面将详细介绍公安室内射击场的设计要点。 首先&#xff0c;安全性是设计公安室内射击场的最重要因素之一。射击场应该具备…

杂记——19.git上传时出现the remote end hung up unexpectedly错误

git是大家常用的项目版本控制工具&#xff0c;熟练地使用git可以提高开发效率&#xff0c;但是有时在使用git推送代码时&#xff0c;会提示“the remote end hung up unexpectedly”的问题&#xff0c;那么git推送代码提示“the remote end hung up unexpectedly”怎么解决呢&a…

Java多线程还不会的进来吧,为你量身打造

&#x1f497;推荐阅读文章&#x1f497; &#x1f338;JavaSE系列&#x1f338;&#x1f449;1️⃣《JavaSE系列教程》&#x1f33a;MySQL系列&#x1f33a;&#x1f449;2️⃣《MySQL系列教程》&#x1f340;JavaWeb系列&#x1f340;&#x1f449;3️⃣《JavaWeb系列教程》…

Anaconda的安装及使用

Anaconda集成了常用的扩展包&#xff0c;能够方便地对这些扩展包进行管理&#xff0c;比如安装和卸载包&#xff0c;这些操作都需要依赖conda。conda是一个在Windows、Mac OS和Linux上运行的开源软件包管理系统和环境管理系统&#xff0c;可以快速地安装、运行和更新软件包及其…

升压模块直流隔离低压转高压稳压电源5v12v24v转50V100V110V150V200V250V400V500V600V800V1000V

特点效率高达80%以上1*2英寸标准封装单电压输出价格低稳压输出工作温度: -40℃~85℃阻燃封装&#xff0c;满足UL94-V0 要求温度特性好可直接焊在PCB 上应用HRB W2~40W 系列模块电源是一种DC-DC升压变换器。该模块电源的输入电压分为&#xff1a;4.5~9V、9~18V、及18~36VDC标准&…