C++STL笔记(八):set和multiset详解
set和multiset结构
一.set和multiset的构造函数和析构函数
二.set和multiset的非更易型操作
三.set和multiset的查找操作函数
四.set和multiset的赋值操作
五.set和multiset的迭代器相关函数
六.set和multiset的安插和移除
例1:set运用实例,本程序示例摘自《C++标准库》(第二版)第7.7.4小节
例2:multiset运用实例,本程序示例摘自《C++标准库》(第二版)第7.7.4小节
附例1代码:
#include<iostream>
#include<set>
#include<algorithm>
#include<iterator>
using namespace std;int main()
{//type of the collection://~no duplicates//~elements are integral values
//-descending orderset<int,greater<int>> coll1;//insert elements in random order using difference member functionscoll1.insert({4,3,5,1,6,2});coll1.insert(5);//printf all elementsfor(int elem:coll1) {cout<<elem<<' ';}cout<<endl;//insert 4 again and process return value_compauto status = coll1.insert(4);if(status.second) {cout<<"4 inserted as element"<<distance(coll1.begin(),status.first)+1<<endl;} else {cout<<"4 already exists"<<endl;}//assign elements to another set with ascending orderset<int>coll2(coll1.cbegin(),coll1.cend());//print all elements of the copy using stream iteratorscopy(coll2.cbegin(),coll2.cend(),ostream_iterator<int>(cout," "));cout<<endl;//remove all elements up to element with value 3coll2.erase(coll2.begin(),coll2.find(3));//remove all elements with value 5int num;num=coll2.erase(5);cout<<num<<" element(s) removed"<<endl;//printf all elementscopy(coll2.cbegin(),coll2.cend(),ostream_iterator<int>(cout," "));cout<<endl;
}
附例2代码:
#include<iostream>
#include<set>
#include<algorithm>
#include<iterator>
using namespace std;int main()
{multiset<int,greater<int>> coll1;coll1.insert({4,3,5,1,6,2});coll1.insert(5);for(int elem:coll1) {cout<<elem<<' ';}cout<<endl;auto ipos=coll1.insert(4);cout<<"4 inserted as element "<<distance(coll1.begin(),ipos)+1<<endl;multiset<int>coll2(coll1.cbegin(),coll1.cend());copy(coll2.cbegin(),coll2.cend(),ostream_iterator<int>(cout," "));cout<<endl;coll2.erase(coll2.begin(),coll2.find(3));int num;num=coll2.erase(5);cout<<num<<" element(s) removed"<<endl;`copy(coll2.cbegin(),coll2.cend(),ostream_iterator<int>(cout," "));cout<<endl;
}
七.运行期指定排序准则
例:本程序示例摘自《C++标准库》(第二版)第7.7.5小节
附上例代码:
#include<iostream>
#include<set>
using namespace std;// PRINT_ELEMENTS()
// - prints optional string optstr followed by
// - all elements of the collection coll
// - in one line, separated by spaces
template <typename T>
inline void PRINT_ELEMENTS (const T& coll,const std::string& optstr="")
{std::cout << optstr;for (const auto& elem : coll) {std::cout << elem << ' ';}std::cout << std::endl;
}//type for runtime sorting criterion
class RuntimeCmp
{public:enum cmp_mode{normal,reverse};
private:cmp_mode mode;
public://constructor for sorting criterion//-default criterion uses value normalRuntimeCmp(cmp_mode m=normal):mode(m){}//comparison of elements//~member function for any element typetemplate<typename T>bool operator()(const T& t1,const T& t2)const{return mode==normal?t1<t2:t2<t1;}//comparison of sorting criteriabool operator==(const RuntimeCmp& rc)const{return mode==rc.mode;}
};//type of a set that uses this sorting criterion
typedef set<int,RuntimeCmp>IntSet;int main()
{//creat,fill,and print set with normal element order//~uses default sorting criterionIntSet coll1={4,7,5,1,6,2,5};PRINT_ELEMENTS(coll1,"coll1: ");//creat sorting criterion with reverse element orderRuntimeCmp reverse_order(RuntimeCmp::reverse);//creat,fill,and print set with reverse element orderIntSet coll2(reverse_order);coll2={4,7,5,1,6,2,5};PRINT_ELEMENTS(coll2,"coll2: ");//assign element AND sorting criterioncoll1=coll2;coll1.insert(3);PRINT_ELEMENTS(coll1,"coll1: ");//just to make sureif(coll1.value_comp()==coll2.value_comp()){cout<<"coll1 and coll2 have ths same sorting criterion"<<endl;}else{cout<<"coll1 and coll2 have a different sorting criterion"<<endl;}
}
C++STL笔记(八):set和multiset详解相关推荐
- C++STL笔记(七):forward list详解
forward list结构 一.forward list的构造函数和析构函数 二.forward list的非更易型操作 三.forward list的赋值操作 四.forward list元素的直 ...
- STL之七:STL各种容器的使用时机详解
C++标准程序库提供了各具特长的不同容器.现在的问题是:该如何选择最佳的容器类别?下表给出了概述. 但是其中有些描述可能不一定实际.例如:如果你需呀处理的元素数量很少,可以虎落复杂度,因为线性算法通常 ...
- ROS入门笔记(七):详解ROS文件系统
ROS入门笔记(七):详解ROS文件系统 文章目录 01 Catkin编译系统 1.1 Catkin特点 1.2 Catkin工作原理 1.3 使用`catkin_make`进行编译 02 Catki ...
- STL迭代器(iterator)用法详解
C++ STL迭代器(iterator)用法详解 无论是序列容器还是关联容器,最常做的操作无疑是遍历容器中存储的元素,而实现此操作,多数情况会选用"迭代器(iterator)"来实 ...
- IP地址和子网划分学习笔记之《子网划分详解》
一,子网划分概述 IP地址和子网划分学习笔记相关篇章: 1.IP地址和子网划分学习笔记之<预备知识:进制计数> 2.IP地址和子网划分学习笔记之<IP地址详解> 3.IP地址和 ...
- Android进阶笔记:Messenger源码详解
Messenger可以理解为一个是用于发送消息的一个类用法也很多,这里主要分析一下再跨进程的情况下Messenger的实现流程与源码分析.相信结合前面两篇关于aidl解析文章能够更好的对aidl有一个 ...
- 2接口详解_TS入门笔记2——TS接口进阶详解
TS入门笔记--TS接口进阶详解 一.为什么需要接口? let obj:object; // 定义了一个只能保存对象的变量 // obj = 1; // obj = "123"; ...
- JDBC学习笔记02【ResultSet类详解、JDBC登录案例练习、PreparedStatement类详解】
黑马程序员-JDBC文档(腾讯微云)JDBC笔记.pdf:https://share.weiyun.com/Kxy7LmRm JDBC学习笔记01[JDBC快速入门.JDBC各个类详解.JDBC之CR ...
- 信号与系统考研复习例题详解_小语种日语日本文学复习考研资料加藤周一《日本文学史序说(上)》笔记和考研真题详解...
加藤周一<日本文学史序说(上)>笔记和考研真题详解 目录 序章 日本文学的特征 0.1 复习笔记 第一章 <万叶集>的时代 1.1 复习笔记 1.2 考研真题与典型题详解 第二 ...
- 小猫爪:i.MX RT1050学习笔记26-RT1xxx系列的FlexCAN详解
i.MX RT1050学习笔记26-RT1xxx系列的FlexCAN详解 1 前言 2 FlexCAN简介 2.1 MB(邮箱)系统 2.1.1 正常模式下 2.1.2 激活了CAN FD情况下 2. ...
最新文章
- QT在windows平台安装使用MInGW编译
- android phpmyadmin,从android studio向phpmyadmin添加数据时出现问题
- windos命令行设置网络
- LLVM(Low Level Virtual Machine)
- 创建一个学生信息表,与页面分离
- matlab 图像处理 新浪 应变,[转载]Matlab图像处理小结
- Gitlab代码库自动同步到Github
- [教程] 虚拟机安装MAC lion 10.7.3教程 附送原版提取镜像! 安装
- go-cqhttp系列教程-三种消息类型的复述(私聊,群聊,频道)-3
- 决策树系列(四)——基于决策树算法实现员工离职率预测
- 傻瓜攻略(十六)——MATLAB实现txt文件复杂内容的读取
- 史上最全Python入门基础教程(非常详细)
- 【软件测试学习笔记】接口自动化测试基础-Day1 网络协议 2020-09-21
- 【接口篇 / Lan】(5.6) ❀ 05. 与思科交换机三层链路聚合连接 ❀ FortiGate 防火墙
- 企业服务是移动互联网的下一个风口
- centos 7 vmstat命令详解
- SQL入门SQL基本介绍
- Godot全局插件支持库
- 不冷不热之人传略《狂热的爱》
- Git - 提交时没有权限解决方法