c++11 标准模板(STL)(std::multiset)(二)

news2024/9/24 21:21:52
定义于头文件 <set>
template<

    class Key,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<Key>

> class multiset;
(1)
namespace pmr {

    template <class Key, class Compare = std::less<Key>>
    using multiset = std::multiset<Key, Compare,
                                   std::pmr::polymorphic_allocator<Key>>;

}
(2)(C++17 起)

std::multiset 是含有 Key 类型对象有序集的容器。不同于 set ,它允许多个关键拥有等价的值。用关键比较函数 Compare 进行排序。搜索、插入和移除操作拥有对数复杂度。

在标准库使用比较 (Compare) 概念的每处,都用描述于比较 (Compare) 的等价关系确定等价性。不精确地说,若二个对象 ab 互不比较小于对方: !comp(a, b) && !comp(b, a) ,则认为它们等价。

比较等价的元素顺序是插入顺序,而且不会更改。(C++11 起)

成员函数

构造函数

std::multiset<Key,Compare,Allocator>::multiset
multiset();

explicit multiset( const Compare& comp,

                   const Allocator& alloc = Allocator() );
(1)

explicit multiset( const Allocator& alloc );

(1)(C++11 起)
template< class InputIt >

multiset( InputIt first, InputIt last,
          const Compare& comp = Compare(),

          const Allocator& alloc = Allocator() );
(2)
template< class InputIt >

multiset( InputIt first, InputIt last,

          const Allocator& alloc );
(C++14 起)

multiset( const multiset& other );

(3)

multiset( const multiset& other, const Allocator& alloc );

(3)(C++11 起)

multiset( multiset&& other );

(4)(C++11 起)

multiset( multiset&& other, const Allocator& alloc );

(4)(C++11 起)
multiset( std::initializer_list<value_type> init,

          const Compare& comp = Compare(),

          const Allocator& alloc = Allocator() );
(5)(C++11 起)

multiset( std::initializer_list<value_type> init,
          const Allocator& );

(C++14 起)

从各种数据源构造新容器,可选地使用用户提供的分配器 alloc 或比较函数对象 comp

1) 构造空容器。

2) 构造容器,使之拥有范围 [first, last) 的内容。

3) 复制构造函数。构造容器,使之拥有 other 的内容副本。若不提供 alloc ,则通过调用 std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) 获得分配器。

4) 移动构造函数。用移动语义构造容器,使之拥有 other 的内容。若不提供 alloc ,则从属于 other 的分配器移动构造分配器

5) 构造容器,使之拥有 initializer_list init 的内容。

参数

alloc-用于此容器所有内存分配的分配器
comp-用于所有关键比较的比较函数对象
first, last-复制元素来源的范围
other-要用作源以初始化容器元素的另一容器
init-用以初始化容器元素的 initializer_list
类型要求
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
- Compare 必须满足比较 (Compare) 的要求。
- Allocator 必须满足分配器 (Allocator) 的要求。

复杂度

1) 常数。

2) N log(N) ,其中通常有 N = std::distance(first, last) ,若范围已为 value_comp() 所排序则与 N 成线性。

3) 与 other 的大小成线性。

4) 常数。若给定 alloc 且 alloc != other.get_allocator() 则为线性。

5) N log(N) ,其中通常有 N = init.size()) ,若 init 已按照 value_comp() 排序则与 N 成线性 。

异常

Allocator::allocate 的调用可能抛出。

注意

在容器移动构造(重载 (4) )后,指向 other 的引用及迭代器(除了尾迭代器)保持合法,但指代现于 *this 中的元素。当前标准由 [container.requirements.general]/12 中的总括陈述作出此保证,而 LWG 2321 正在考虑更严格的保证。

 

析构函数

std::multiset<Key,Compare,Allocator>::~multiset

~multiset();

销毁容器。调用元素的析构函数,然后解分配所用的存储。注意,若元素是指针,则不销毁所指向的对象。

复杂度

与容器大小成线性。

 

调用示例

#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <time.h>
#include <set>

using namespace std;

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

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

int main()
{
    std::cout << std::boolalpha;

    //1) 构造空容器。
    std::multiset<Cell> multiset1;
    std::cout << "multiset1 is empty " << multiset1.empty() << std::endl;
    std::cout << std::endl;


    std::vector<Cell> vector1{{101, 101}, {102, 102}, {103, 103}, {104, 104}, {105, 105}};

    //2) 构造容器,使之拥有范围 [first, last) 的内容。
    std::multiset<Cell> multiset2(vector1.begin(), vector1.end());
    std::cout << "multiset2:    ";
    std::copy(multiset2.begin(), multiset2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::multiset<Cell, std::greater<Cell>> multiset3(vector1.begin(), vector1.end());
    std::cout << "multiset3:    ";
    std::copy(multiset3.begin(), multiset3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    //3) 复制构造函数。构造容器,使之拥有 other 的内容副本。
    std::multiset<Cell> multiset4(multiset2);
    std::cout << "multiset4:    ";
    std::copy(multiset4.begin(), multiset4.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::multiset<Cell, std::greater<Cell>> multiset5(multiset3);
    std::cout << "multiset5:    ";
    std::copy(multiset5.begin(), multiset5.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    //4) 移动构造函数。用移动语义构造容器,使之拥有 other 的内容。
    std::multiset<Cell> multiset6(std::move(multiset2));
    std::cout << "multiset6:    ";
    std::copy(multiset6.begin(), multiset6.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::multiset<Cell, std::greater<Cell>> multiset7(std::move(multiset3));
    std::cout << "multiset7:    ";
    std::copy(multiset7.begin(), multiset7.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    //5) 构造容器,使之拥有 initializer_list init 的内容。
    std::multiset<Cell> multiset8{{101, 101}, {102, 102}, {103, 103}, {104, 104}, {105, 105}};
    std::cout << "multiset8:    ";
    std::copy(multiset8.begin(), multiset8.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::multiset<Cell, std::greater<Cell>> multiset9{{101, 101}, {102, 102}, {103, 103}, {104, 104}, {105, 105}};
    std::cout << "multiset9:    ";
    std::copy(multiset9.begin(), multiset9.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    return 0;
}

输出

 

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

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

相关文章

Day12【元宇宙的实践构想01】—— 元宇宙概念和发展历程

&#x1f483;&#x1f3fc; 本人简介&#xff1a;男 &#x1f476;&#x1f3fc; 年龄&#xff1a;18 ✍每日一句&#xff1a;【道固远&#xff0c;笃行可至&#xff1b;事虽巨&#xff0c;坚为必成】 &#x1f6a9; 今日留言&#xff1a;亮亮被迫去练科目二啦&#xff0c;定时…

K8S架构熟悉及日常操作

目录 一、架构介绍 二、组件介绍 三、调度介绍 四、CLI指令介绍 五、常见CLI指令 六、常见问题排查思路 一、架构介绍 Kubernetes系统架构为客户端/服务端&#xff08;C/S&#xff09;架构&#xff0c;Master作为服务端&#xff0c;Node作为客户端。 Master服务端也被称…

学习逆向安全的必备基础: 汇编的初步了解

什么是汇编 汇编语言是一种低级编程语言&#xff0c;它使用简单的助记符来表示计算机底层的机器指令。 汇编语言是直接与计算机硬件交互的&#xff0c;它能够控制计算机中的每一个细节。 由于汇编语言非常低级&#xff0c;所以编写汇编程序通常比较困难。不过&#xff0c;汇…

微信怎样开发小程序【公司企业小程序开发】

现在很多公司企业都有自己的小程序&#xff0c;没有小程序的公司企业也会寻找开发小程序的途径。那么今天就给大家简单介绍微信怎样开发小程序&#xff0c;希望对需要开发小程序的公司企业有帮助。 一、注册小程序账号 有一个小程序账号是必须的&#xff0c;小程序账号可以在…

那些外贸老鸟们都在认真使用的8个实用小工具

在我们日常的外贸工作中&#xff0c;有很多地方都可以用到一些实用外贸小工具去提高工作效率&#xff0c;突破局部限制。是否能够灵活的应用这些实用外贸小工具&#xff0c;是一位成熟优秀外贸业务员的衡量标准之一。第一个&#xff1a;知识信息整理和CRMhttps://www.notion.so…

Linux(六)基础I/O

引言 C语言进阶 文件管理 上一篇文章详细回顾了C语言方面关于文件操作的一些库函数&#xff0c;比如输入输出重定向fscanf、fprintf&#xff0c;对于文件内容以字符形式读取的fgetc、fputc&#xff0c;对于文件内容以字符串形式读取的fgets、fputs&#xff0c;对于二进制文件的…

ieee会议论文从手稿到发表

0. 前言 在创新点得到认可之后就可以准备发论文了&#xff0c;这个一定要早点&#xff0c;可以给自己设置一个明确的deadline&#xff0c;毕竟ddl是第一生产力。 1. 确定发什么期刊、会议 一定要符合学校的毕业要求&#xff0c;有一些水的学校并不认。时间看能不能赶上学校毕…

物联网智慧消防对比传统消防具有哪些优势?

随着科技的进步和城市化进程的加快&#xff0c;传统消防已经满足不了社会发展的需求&#xff0c;智慧消防应运而生&#xff0c;目前智慧消防已经成为消防安全管理的核心&#xff0c;物联网时代的到来&#xff0c;让智慧消防迎来了更大的发展机遇&#xff0c;变得更加智慧化、系…

手机网站建设怎么做?【手机网站制作】

对于很多公司企业来说&#xff0c;做网站建设都是优先考虑PC端的网站建设&#xff0c;但是某些公司企业可能对于PC端网站的需求不高&#xff0c;倒是更有需要做移动端网站&#xff0c;也就是我们常说的手机网站。那么关于手机网站建设又是怎么做的呢&#xff1f;本文给大家做一…

软件测试员年底总结怎么写?所有问题都帮你梳理好了!

临近年底&#xff0c;很多公司都有年终总结环节&#xff0c;核心目的发现今年的不足&#xff0c;进而总结经验&#xff0c;更好地用以指导明年的工作。当然&#xff0c;即使公司没有要求&#xff0c;对于测试岗位来说&#xff0c;一年一度的总结不可或缺。假如你是测试负责人&a…

<使用Python自定义生成简易二维码>——《Python项目实战》

目录 1.问题导引 2.实现步骤 &#xff08;1&#xff09;查找并安装第三方库qrcode &#xff08;2&#xff09;编写代码并嵌入内置信息 &#xff08;3&#xff09;使用扫码工具读取信息 后记&#xff1a;●由于作者水平有限&#xff0c;文章难免存在谬误之处&#xff0c;敬…

【Tkinter】终于把StringVar讲明白了

文章目录简介Label使用StringVarEntry输入简介 初学者在使用tkinter时常犯的一个错误就是 def changeText(evt):evt.text "new Text"毕竟在创建控件时&#xff0c;text是出现频率很高的参数&#xff0c;换言之&#xff0c;我们会默认控件中有text这个属性&#xf…

CDGA/CDGP数据治理认证班将于2/4正式开课,报名从速!

新的一年&#xff0c;从考证开始&#xff0c;为职场竞争增添更多优势&#xff01; 做数据行业的话&#xff0c;当然推荐考个DAMA-CDGA/CDGP数据管理证书啦&#xff01; DAMA是全球唯一数据管理方面权威性认证&#xff0c;帮助数据从业者提升数据管理能力。 DAMA认证为数据管理专…

【c语言进阶】文件操作(下)

&#x1f680;write in front&#x1f680; &#x1f4dc;所属专栏&#xff1a; c语言学习 &#x1f6f0;️博客主页&#xff1a;睿睿的博客主页 &#x1f6f0;️代码仓库&#xff1a;&#x1f389;VS2022_C语言仓库 &#x1f3a1;您的点赞、关注、收藏、评论&#xff0c;是对我…

[NOIP2008 提高组] 笨小猴

题目描述 笨小猴的词汇量很小&#xff0c;所以每次做英语选择题的时候都很头疼。但是他找到了一种方法&#xff0c;经试验证明&#xff0c;用这种方法去选择选项的时候选对的几率非常大&#xff01; 这种方法的具体描述如下&#xff1a;假设 maxn 是单词中出现次数最多的字母的…

Python 中当前时间表示方法详解

在 Python 中获取当前时间是许多与时间有关的操作的一个很好的起点。一个非常重要的用例是创建时间戳。在本教程中&#xff0c;你将学习如何用 datetime 模块获取、显示和格式化当前时间。我们将学习如何读取当前时间的属性&#xff0c;如年份、分钟或秒。为了使时间更容易阅读…

函数的求导法则——“高等数学”

今天&#xff0c;小雅兰的内容是函数的求导法则&#xff0c;上篇博客我们知道了导数的定义、导数的几何意义及可导与连续关系&#xff0c;这篇博客我们来仔细学习一下求导法则&#xff0c;下面&#xff0c;就让我们进入导数的世界吧 一、函数的和、差、积、商的求导法则 二、反…

Beryl Li 代表 YGG 出席 2023 年世界经济论坛会议

Yield Guild Games&#xff08;YGG&#xff09;联合创始人 Beryl Li 代表 YGG 参加了 2023 年 1 月 16 日至 20 日在瑞士达沃斯举行的 2023 年世界经济论坛年会 &#xff08;WEF23&#xff09;&#xff0c;在全球舞台上分享区块链、通证化、数字资产监管、治理和价值创造的潜力…

C++语法复习笔记-4. C++基本容器

文章目录1.数组声明与定义数组的开闭区间差一错误左闭右开非对称区间原则数组的增删改查一维数组二维数组面向对象的动态数组-vector自动扩容增删改查2. 字符串字符串变量与常量unicode编码字符串指针表示方法指针的表示方法char[]和char* 的区别数组每个值可改指针指向的字符串…

Yann LeCun 新作!大幅超越 MAE,图像语义表示卷出新高度

文&#xff5c;CV酱计算机视觉中&#xff0c;有两种常见的从图像中进行自我监督学习的方法&#xff1a;基于不变性的方法和生成方法。基于不变性的预训练方法优化编码器&#xff0c;使其产生相似的嵌入&#xff0c;用于同一图像的两个或多个视图&#xff0c;其中图像视图通常使…