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>::empty

bool empty() const;

(C++11 前)

bool empty() const noexcept;

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

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

(C++20 起)

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

参数

(无)

返回值

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

复杂度

常数。

返回容纳的元素数

std::map<Key,T,Compare,Allocator>::size

size_type size() const;

(C++11 前)

size_type size() const noexcept;

(C++11 起)

 返回容器中的元素数,即 std::distance(begin(), end()) 。

参数

(无)

返回值

容器中的元素数量。

复杂度

常数。

返回可容纳的最大元素数

std::map<Key,T,Compare,Allocator>::max_size

size_type max_size() const;

(C++11 前)

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 <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()
{
    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;
    //检查容器是否无元素,即是否 begin() == end() 。
    std::cout << "map1 is empty:   " << map1.empty() << std::endl;

    for (size_t index = 0; index < 5; index++)
    {
        map1.insert({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;

    //检查容器是否无元素,即是否 begin() == end() 。
    std::cout << "map1 is empty:   " << map1.empty() << std::endl;
    std::cout << std::endl;
    std::cout << std::endl;

    std::map<int, Cell> map2;
    for (size_t index = 0; index < 6; index++)
    {
        //返回容器中的元素数,即 std::distance(begin(), end()) 。
        std::cout << "map2 size :  " << map2.size() << ": ";
        std::copy(map2.begin(), map2.end(),
                  std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
        std::cout << std::endl;
        map2.insert({genKey(), generate()});
    }
    std::cout << std::endl;


    //返回根据系统或库实现限制的容器可保有的元素最大数量,即对于最大容器的 std::distance(begin(), end()) 。
    std::map<bool, bool> mapbb;
    std::cout << "map<bool, bool> max_size:                        " << mapbb.max_size() << std::endl;
    std::map<char, char> mapcc;
    std::cout << "map<char, char> max_size:                        " << mapcc.max_size() << std::endl;
    std::map<short, short> mapss;
    std::cout << "map<short, short> max_size:                      " << mapss.max_size() << std::endl;
    std::map<unsigned short, unsigned short> mapusus;
    std::cout << "map<unsigned short, unsigned short> max_size:    " << mapusus.max_size() << std::endl;
    std::map<int, int> mapii;
    std::cout << "map<int, int> max_size:                          " << mapii.max_size() << std::endl;
    std::map<uint8_t, uint8_t> mapui8ui8;
    std::cout << "map<uint8_t, uint8_t> max_size:                  " << mapui8ui8.max_size() << std::endl;
    std::map<uint16_t, uint16_t> mapui16ui16;
    std::cout << "map<uint16_t, uint16_t> max_size:                " << mapui16ui16.max_size() << std::endl;
    std::map<uint32_t, uint32_t> mapui32ui32;
    std::cout << "map<uint32_t, uint32_t> max_size:                " << mapui32ui32.max_size() << std::endl;
    std::map<uint64_t, uint64_t> mapui64ui64;
    std::cout << "map<uint64_t, uint64_t> max_size:                " << mapui64ui64.max_size() << std::endl;
    std::map<double, double> mapdd;
    std::cout << "map<double, double> max_size:                    " << mapdd.max_size() << std::endl;
    std::map<float, float> mapff;
    std::cout << "map<float, float> max_size:                      " << mapff.max_size() << std::endl;
    std::map<long, long> mapll;
    std::cout << "map<long, long> max_size:                        " << mapll.max_size() << std::endl;
    std::map<long long, long long> mapllll;
    std::cout << "map<long long, long long> max_size:              " << mapllll.max_size() << std::endl;
    std::map<string, string> mapstrstr;
    std::cout << "map<string, string> max_size:                    " << mapstrstr.max_size() << std::endl;
    std::map<Cell, Cell> mapcell;
    std::cout << "map<Cell, Cell> max_size:                        " << mapcell.max_size() << std::endl;

    return 0;
}

输出

 

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

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

相关文章

【Linux驱动】认识驱动(驱动的概念、驱动分类)

目录 1、什么是驱动&#xff1f; 2、应用程序调用驱动基本流程 3、file_operations 结构体 4、驱动的分类 1、什么是驱动&#xff1f; 驱动就是一段程序&#xff0c;能够获取外设或者传感器数据、控制外设。驱动获取到的数据会提交给应用程序。 在 Linux 中一切皆为文件&…

物联网GPRS模块流量计算

物联网GPRS模块流量计算 MQTT(消息队列遥测传输) 是ISO 标准下一个基于TCP/IP的消息发布/订阅传输协议。 一、TCP消耗流量计算 以太网数据包结构&#xff1a; 以太网首部 IP首部 TCP首部 APPL首部 用户数据 以太网尾部 以太网首部为14个字节 IP首部为20个字节 TCP首部…

【CesiumJS入门】(1)创建Viewer及相关配置项

前言 在上一篇博客中&#xff0c;我们直接在vue组件完成初始渲染并创建 DOM 节点后通过 const map new Cesium.Viewer(cesiumContainer)构建了一个地球场景。 而本篇&#xff0c;我们将会专门把地球创建的方法写在一个js文件中&#xff0c;以便后续的调用。 同时&#xff0…

【JavaSE】Java基础语法(三十二):Stream流

文章目录 1. 体验Stream流2. Stream流的常见生成方式3. Stream流中间操作方法【应用】4. Stream流终结操作方法【应用】5. Stream流的收集操作 1. 体验Stream流 案例需求 按照下面的要求完成集合的创建和遍历 创建一个集合&#xff0c;存储多个字符串元素把集合中所有以"…

torch_scatter.scatter()的使用方法

学习目标&#xff1a; 在学习PyG时&#xff0c;遇到了 scatter 这个函数&#xff0c;经过学习加上自身的理解&#xff0c;记录如下以备复习 学习内容&#xff1a; src&#xff1a;表示输入的tensor&#xff0c;接下来被处理&#xff1b;index&#xff1a;表示tensor对应的索引…

机器学习 day14 ( 神经网络 )

神经网络的发展 最开始的动机&#xff1a;是通过构建软件来模拟大脑&#xff0c;但今天的神经网络几乎与大脑的学习方式无关 我们依据大脑中的神经网络&#xff0c;来构建人工神经网络模型。左图中&#xff1a;一个神经元可以看作一个处理单元&#xff0c;它有很多的输入/树突…

chatgpt赋能python:Python创建一个Animal类介绍

Python创建一个Animal类介绍 Python是一种高级编程语言&#xff0c;其简单易学、灵活性强、可读性高以及强大的库使得Python非常受欢迎。在Python中创建类非常容易且非常常见&#xff0c;我们可以使用Python创建各种类型的类。今天&#xff0c;我们将讨论如何使用Python创建一…

【时空权衡】

目录 知识框架No.0 时空权衡一、基本思想 No.1 计数排序一、比较计数二、分布计数 No.2 散列法一、开散列&#xff08;分离链&#xff09;二、闭散列&#xff08;开式寻址&#xff09; 知识框架 No.0 时空权衡 一、基本思想 其实时空权衡&#xff1a;是指在算法的设计中&…

进程信号(Linux)

进程信号 信号入门身边的信号进程信号 产生信号终端按键产生信号调用系统函数向目标进程发信号killraiseabort 硬件异常产生信号由软件条件产生信号 阻塞信号信号其他相关常见概念在内核中的表示sigset_t信号集操作函数sigprocmasksigpending 捕捉信号内核如何实现信号的捕捉si…

分布式简要说明

1.分布式简要说明 《分布式系统原理与范型》定义&#xff1a; 分布式系统是若干独立计算机的集合&#xff0c;这些计算机对于用户来说就像单个相关系统。 分布式系统 (distributed system) 是建立在网络之上的软件系统。 随着互联网的发展&#xff0c;网站应用的规模不断扩…

SAP-QM-物料主数据-质量管理视图字段解析

过账到质检库存&#xff1a;要勾选&#xff0c;否则收货后库存不进入质检库存HU检验&#xff1a;收货到启用HU管理的库位时产生检验批&#xff0c;例如某个成品物料是收货到C002库位&#xff0c;该库位启用了HU管理&#xff0c;那么此处要勾选。但是如果勾选了&#xff0c;却收…

04.hadoop上课笔记之java编程和hbase

1.win查看服务 netstat -an #linux也有#R数学建模语言 SCALAR 2.java连接注意事项,代码要设置用户 System.setProperty("HADOOP_USER_NAME", "hadoop");3.伪分布式的好处(不用管分布式细节,直接连接一台机器…,适合用于学习) 4.官方文档 查看类(static |…

Python期末复习题库(下)——“Python”

小雅兰期末加油冲冲冲&#xff01;&#xff01;&#xff01; 1. (单选题)下列关于文件打开模式的说法,错误的是( C )。 A. r代表以只读方式打开文件 B. w代表以只写方式打开文件 C. a代表以二进制形式打开文件 D. 模式中使用时,文件可读可写 2. (单选题)下列选项中,以追加…

webpack的使用

一、什么是webpack&#xff1f; webpack是一个前端构建工具&#xff0c;目前比较主流的构建工具&#xff0c;自定义的模块比较多。 二、应用场景 vue、react、angular 都可以通过webpack构建全部可供访问的页面数量不超过500个 三、安装 通过npm方式在项目根目录下执行命令…

htmlCSS-----CSS选择器(下)

目录 前言&#xff1a; 2.高级选择器 &#xff08;1&#xff09;子代选择器 &#xff08;2&#xff09;伪类选择器 &#xff08;3&#xff09;后代选择器 &#xff08;4&#xff09;兄弟选择器 相邻兄弟选择器 通用兄弟选择器 &#xff08;5&#xff09;并集选择器 &am…

【JavaSE】Java基础语法(二十六):Collection集合

文章目录 1. 数组和集合的区别2. 集合类体系结构3. Collection 集合概述和使用【应用】4. Collection集合的遍历【应用】5. 增强for循环【应用】 1. 数组和集合的区别 相同点 都是容器,可以存储多个数据不同点 数组的长度是不可变的,集合的长度是可变的 数组可以存基本数据类型…

基于Yarn搭建Flink

基于Yarn搭建Flink 1. 概述 1.1 Yarn 简介 Apache Hadoop YARN是一个资源提供程序&#xff0c;受到许多数据处理框架的欢迎。Flink服务被提交给 YARN 的 ResourceManager&#xff0c;后者再由 YARN NodeManager 管理的机器上生成容器。Flink 将其 JobManager 和 TaskManager…

python爬虫笔记

Python爬虫笔记 一. Urllib 1. 基础请求 指定url请求返回值解码返回结果的一些操作 import urllib.request as req # 定义一个url url http://www.baidu.com# 发送请求获得相应 res req.urlopen(url)# read返回字节形式的二进制数据,需要用指定编码来解码 content res.r…

vue的虚拟DOM

vue的虚拟DOM 什么是虚拟DOM 虚拟DOM提供了一个与平台无关的抽象层&#xff0c;将应用程序的界面表示抽象为一个虚拟的DOM树。这意味着开发人员可以使用相同的代码和逻辑来描述应用程序的用户界面&#xff0c;而不需要关心具体的平台实现细节。虚拟DOM允许开发人员使用一种统…

Linux命令

准备工作 安装centos7 在镜像网下载DVD.iso或者DVD.torrent&#xff08;bit种子&#xff09;。在VMware中配置相应的信息&#xff0c;并引入iso文件&#xff0c;以便后续安装。local中&#xff1a;选择语言和时区上海software中&#xff1a;选择安装软件的内容&#xff0c;可…