std::multiset

介绍

成员函数

  • 构造析构
  • 元素访问
  • 迭代器
  • 容量
  • 修改器
  • 查找

非成员函数

介绍

// multiset 模板定义
template<class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key>> class multiset;
namespace pmr {template< class Key, class Compare = std::less<Key> >using multiset = std::multiset<Key, Compare, std::pmr::polymorphic_allocator<Key>>;
}
  • std::multiset 介绍摘选自 cppreference.com 中文网 std::multiset 介绍
  • std::multiset 是含有 Key 类型对象有序集的容器
  • 与 set 不同,它允许多个 Key 拥有等价的值
  • 用关键比较函数 Compare 进行排序。搜索、插入和移除操作拥有对数复杂度
  • 标准库使用比较 (Compare) 概念时用等价关系确定唯一性
  • 不精确地说,如果两个对象 a 与 b 相互不比较小于对方:!comp(a, b) && !comp(b, a),那么认为它们等价
  • 比较等价的元素顺序是插入顺序,而且不会更改

成员函数

构造析构
#include <QCoreApplication>
#include <algorithm>
#include <iostream>
#include <set>
#include <string>auto Print(const std::string &msg, const std::multiset<int> &lst) {std::cout << msg << " : ";for (const auto &pa : lst) {std::cout << pa << "\t";}std::cout << "\n";
}int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);using set_int = std::multiset<int>;set_int s1; //默认构造函数Print("s1", s1);std::less<int> cmp;std::allocator<int> alloc;set_int s2(cmp); //设置比较器Print("s2", s2);set_int s3(cmp, alloc); //设置比较器和分配器Print("s3", s3);set_int s4(alloc); //设置分配器Print("s4", s4);set_int s5{1, 1, 2, 3, 4, 5}; //初始化列表Print("s5", s5);set_int s6(s5.begin(), s5.end()); //迭代器初始化Print("s6", s6);set_int s7(s6); //拷贝构造函数Print("s7", s7);set_int s8(s7, alloc);Print("s8", s8);set_int s9(std::move(s8)); //移动构造函数Print("s9", s9);set_int s10(std::move(s9), alloc);Print("s10", s10);// 析构函数默认return 0; // a.exec();
}

输出结果:
s1 :
s2 :
s3 :
s4 :
s5 : 1 1 2 3 4 5
s6 : 1 1 2 3 4 5
s7 : 1 1 2 3 4 5
s8 : 1 1 2 3 4 5
s9 : 1 1 2 3 4 5
s10 : 1 1 2 3 4 5

元素访问
int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);using set_int = std::multiset<int>;set_int s1 = {3, 1, 4, 1, 5, 9, 2, 6, 5};std::cout << "s1 : ";std::for_each(s1.cbegin(), s1.cend(), [](int x) { std::cout << x << "\t"; });std::cout << "\n";return 0; // a.exec();
}

输出结果:
s1 : 1 1 2 3 4 5 5 6 9

迭代器
int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);using set_int = std::multiset<int>;set_int s1 = {3, 1, 4, 1, 5, 9, 2, 6, 5};set_int::iterator iter = s1.begin(); //返回指向起始的迭代器std::cout << "s1 : ";for (; iter != s1.end(); ++iter) {std::cout << *iter << "\t";}std::cout << "\n";set_int::const_iterator citer = s1.cbegin(); //返回指向起始的迭代器,conststd::cout << "s1 : ";for (; citer != s1.cend(); ++citer) {std::cout << *citer << "\t";}std::cout << "\n";set_int::reverse_iterator riter = s1.rbegin(); //返回指向起始的逆向迭代器std::cout << "s1 : ";for (; riter != s1.rend(); ++riter) {std::cout << *riter << "\t";}std::cout << "\n";set_int::const_reverse_iterator criter =s1.crbegin(); //返回指向起始的逆向迭代器,conststd::cout << "s1 : ";for (; criter != s1.crend(); ++criter) {std::cout << *criter << "\t";}std::cout << "\n";return 0; // a.exec();
}

输出结果:
s1 : 1 1 2 3 4 5 5 6 9
s1 : 1 1 2 3 4 5 5 6 9
s1 : 9 6 5 5 4 3 2 1 1
s1 : 9 6 5 5 4 3 2 1 1

容量
auto Print(const std::string &msg, const std::multiset<int> &lst) {std::cout << msg << " : ";for (const auto &pa : lst) {std::cout << pa << "\t";}std::cout << "\n";
}int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);using set_int = std::multiset<int>;set_int s1 = {3, 1, 4, 1, 5, 9, 2, 6, 5};Print("s1", s1);//检查容器是否为空std::cout << std::boolalpha << "s1.empty() : " << s1.empty() << std::endl;std::cout << "s1.size() : " << s1.size() << std::endl; //返回容纳的元素数std::cout << "s1.max_size() : " << s1.max_size()<< std::endl; //返回可容纳的最大元素数,和平台有关return 0;               // a.exec();
}

输出结果:
s1 : 1 1 2 3 4 5 5 6 9
s1.empty() : false
s1.size() : 9
s1.max_size() : 576460752303423487

修改器
auto Print(const std::string &msg, const std::multiset<int> &lst) {std::cout << msg << " : ";for (const auto &pa : lst) {std::cout << pa << "\t";}std::cout << "\n";
}int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);using set_int = std::multiset<int>;set_int s1 = {3, 1, 4, 1, 5, 9, 2, 6, 5};Print("s1", s1);s1.clear(); //  清除内容Print("s1", s1);s1.insert(10); //插入元素或结点Print("s1", s1);s1.emplace(20); //原位构造元素Print("s1", s1);s1.emplace_hint(s1.end(), 30); //使用提示原位构造元素Print("s1", s1);s1.erase(s1.begin()); //擦除元素Print("s1", s1);set_int s2;s2.swap(s1);Print("m1", s1);Print("m2", s2);return 0; // a.exec();
}

输出结果:
s1 : 1 1 2 3 4 5 5 6 9
s1 :
s1 : 10
s1 : 10 20
s1 : 10 20 30
s1 : 20 30
m1 :
m2 : 20 30

查找
auto Print(const std::string &msg, const std::multiset<int> &lst) {std::cout << msg << " : ";for (const auto &pa : lst) {std::cout << pa << "\t";}std::cout << "\n";
}int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);using set_int = std::multiset<int>;set_int s1 = {3, 1, 4, 1, 5, 9, 2, 6, 5};Print("s1", s1);std::cout << "s1.count(1) : " << s1.count(1)<< std::endl; //返回匹配特定键的元素数量//寻找键等于 key 的的元素,若容器中有数个拥有键 key 的元素,则可能返回任意一者auto it = s1.find(2); //寻找带有特定键的元素if (it == s1.end()) {std::cout << "s1.find(2) : s1.end()" << std::endl;} else {std::cout << "s1.find(2) : " << *it << std::endl;}//返回容器中所有拥有给定关键的元素范围。范围以二个迭代器定义,一个指向首个不小于// key 的元素,另一个指向首个大于 key 的元素。首个迭代器可以换用 lower_bound()//获得,而第二迭代器可换用 upper_bound() 获得std::pair<set_int::iterator, set_int::iterator> pa =s1.equal_range(5); //返回匹配特定键的元素范围std::cout << *pa.first << std::endl;std::cout << *pa.second << std::endl;set_int::iterator lit =s1.lower_bound(2); //返回指向首个不小于给定键的元素的迭代器std::cout << "m1.lower_bound(2) : " << *lit << std::endl;lit = s1.upper_bound(1); //返回指向首个大于给定键的元素的迭代器std::cout << "m1.upper_bound(1) : " << *lit << std::endl;return 0; // a.exec();
}

输出结果:
s1 : 1 1 2 3 4 5 5 6 9
s1.count(1) : 2
s1.find(2) : 2
5
6
m1.lower_bound(2) : 2
m1.upper_bound(1) : 2

非成员函数

auto Print(const std::string &msg, const std::multiset<int> &lst) {std::cout << msg << " : ";for (const auto &pa : lst) {std::cout << pa << "\t";}std::cout << "\n";
}int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);using set_int = std::multiset<int>;set_int s1 = {3, 1, 4, 1, 5, 9, 2, 6, 5};set_int s2 = {3, 1, 4, 1, 5, 6, 8};Print("s1", s1);Print("s2", s2);std::cout.setf(std::ios::boolalpha);std::cout << "s1 == s2 : " << (s1 == s2) << std::endl;std::cout << "s1 != s2 : " << (s1 != s2) << std::endl;std::cout << "s1 > s2 : " << (s1 > s2) << std::endl;std::cout << "s1 >= s2 : " << (s1 >= s2) << std::endl;std::cout << "s1 < s2 : " << (s1 < s2) << std::endl;std::cout << "s1 <= s2 : " << (s1 <= s2) << std::endl;std::cout.unsetf(std::ios::boolalpha);// c++20 废弃以上操作符重载,提供三路运算符 operator <=> ()std::swap(s1, s2);Print("s1", s1);Print("s2", s2);return 0; // a.exec();
}

输出结果:
s1 : 1 1 2 3 4 5 5 6 9
s2 : 1 1 3 4 5 6 8
s1 == s2 : false
s1 != s2 : true
s1 > s2 : false
s1 >= s2 : false
s1 < s2 : true
s1 <= s2 : true
s1 : 1 1 3 4 5 6 8
s2 : 1 1 2 3 4 5 5 6 9

起始

std::multiset相关推荐

  1. C++ std::multiset返回值 has no member named ‘first’

    error: 'std::multiset<>::iterator {aka struct std::_Rb_tree_const_iterator<>}' has no me ...

  2. C++ STL标准库:std::set std::multiset 插入元素insert() 擦除元素erase()的使用

    文章目录 std::set::erase std::set::insert 见: https://tangxing.blog.csdn.net/article/details/111310069 st ...

  3. STL之multiset中equal_range,multimap中的equal_range,bitset容器,string字符串操作,lambda表达式

     1multiset中用equal_range来遍历所有的元素 #include <set> #include <iostream> using namespace std ...

  4. HDU-Yuna's confusion 树状数组 Or Multiset

    比赛的时候竟然没有想到这题可以用树状数组过,由于数字的区间比较小,所以直接开设一个树状数组,定义sum(i) 表示小于i的数的总数.那么判定一个数是否有的条件就是计算sum(i+1) == sum(i ...

  5. C++multiset集合

    multiset集合容器: ------ 和set的区别:set容器中所有的元素必须独一无二,而multiset容器中元素可以重复 调用头文件: #include<set> using n ...

  6. c++STL标准模板库(关联式容器(set,multiset容器))

    关联式容器(associate容器)是STL提供的容器的一种,其中元素与序列容器不同的是它已经排过序,它主要通过关键字的方式来提高查询效率.关联式容器包含set.multiset.map.multim ...

  7. C++11:右值引用、移动构造、std::move, 以及使用emplace_back代替push_back

    最近在写一段代码的时候,突然很好奇C++11中对push_back有没有什么改进以增加效率,上网搜了一些资料,发现果然新增了emplace_back方法,比push_back的效率要高很多. 1.右值 ...

  8. multiset学习之创建,初始化,赋值操作operator=, empty,size

    本篇开始学习multiset,与set相比,multiset可以有多个相关的关键字,其它也set的功能一样,下面我们先来学习它的创建,初始化,赋值操作operator=, empty,size等相关操 ...

  9. Json文件解析(下

    Json文件解析(下) 代码地址:https://github.com/nlohmann/json 从STL容器转换 任何序列容器(std::array,std::vector,std::deque, ...

最新文章

  1. “封杀中兴”后,TensorFlow 等也被“闭源”怎么办?(附中兴最新声明)
  2. linux下为php添加curl扩展的方法
  3. 使用Arquillian测试安全的EJB
  4. python和java哪个好找工作-2019年Python、Java、C++学哪个更好?薪资更高?
  5. AngularJS 模板
  6. javascript写字技巧_关于 js的一些书写习惯 实用风格 小技巧
  7. 利用词袋模型和TF-IDF实现Large Movie Review Dataset文本分类
  8. 文本文件以EF BB开头,使用UTF8读取
  9. python中素数的求法_python求质数的3种方法
  10. 老罗Android开发视频教程(Android入门介绍)九集集合
  11. 机器学习中的激活函数作用
  12. 精雕软件怎么把图片转成灰度图_精雕软件怎样保存bmp
  13. 【汽车总线技术】CAN FD与CAN网络共存的解决方案
  14. HDU-1869 六度分离 (矩阵的幂运算求通路个数)
  15. 面包屑导航html页面,react怎么实现面包屑导航
  16. 网络防火墙的配置与管理
  17. PPT设置“只读模式”的两种方法
  18. 利用Karabiner和键盘修饰键修改MAC键盘,实现打字时双手不离开字母和数字区
  19. IIS6.0功能及应用详解
  20. verify_area

热门文章

  1. win7和u盘redhat7.1双系统安装总结
  2. 河南分销商城小程序开发打造线上渠道
  3. Telnet的常用命令和开启方式
  4. 最新极光推送在ios模拟器上无法运行
  5. 聚合型新生态模式-分享购,会员及奖励制度
  6. 网易云音乐 Linux 版无法打开的解决方法.
  7. HTML四种定位-绝对定位
  8. matlab newff激活函数,matlab神经网络newff函数的用法
  9. [Kubernetes]谈谈容器跨主机网络
  10. SignalTap II使用技巧之禁用/去除stp文件绑定