set/multiset容器、map容器
目录
set/multiset容器
set基本概念
set大小和交换
set插入和删除
查找和统计
set和multiset的区别
改变set排序规则
set存放内置数据类型
set存放自定义数据类型
pair队组
map容器
map容器的基本概念
map构造和赋值
map大小和交换
map插入和删除
map查找和统计
改变map排序规则
set/multiset容器
set基本概念
简介:
所有元素都会在插入时自动被排序
本质:
set/multiset属于关联式容器,底层结构是用二叉树实现
set和multiset区别:
set不允许容器中有重复的元素
multiset允许容器中有重复的元素
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
using namespace std;void print(const set<int>&L)
{for(set<int>::const_iterator it = L.begin();it!=L.end();it++){cout << *it << " ";}cout << endl;
}int main()
{//set容器特点 所有元素插入时被自动赋值//set容器不允许插入重复值set<int> s1;s1.insert(40);s1.insert(10);s1.insert(20);s1.insert(60);print(s1);set<int> s2(s1);print(s2);set<int> s3;s3 = s1;print(s3);return 0;
}
编译运行
set大小和交换
size();//返回容器中元素的个数
empty();//判断容器是否为空empty();
swap(st);//交换两个集合容器
set插入和删除
insert(elem);//在容器中插入元素
clear();//清除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(beg,end);//删除区间[beg,end)的所有元素,返回下一个元素的选代器
erase(elem);//删除容器中值为elem的元素
查找和统计
find(key);//查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key);//统计key的元素个数
set和multiset的区别
set不允许容器中有重复的元素
multiset允许容器中有重复的元素
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
using namespace std;int main()
{set<int> s1;s1.insert(40);s1.insert(10);s1.insert(20);s1.insert(60);s1.insert(60);for(set<int>::const_iterator it = s1.begin();it!=s1.end();it++){cout << *it << " ";}cout << endl;multiset<int> s2;s2.insert(40);s2.insert(10);s2.insert(20);s2.insert(60);s2.insert(60);for(multiset<int>::const_iterator it = s2.begin();it!=s2.end();it++){cout << *it << " ";}cout << endl;return 0;
}
编译运行
改变set排序规则
set存放内置数据类型
set容器默认排序是从小到大排序 利用仿函数可以改变set容器排序规则
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
using namespace std;class person
{
public:bool operator()(int v1,int v2){return v1 > v2;}
};int main()
{//默认从小到大排序set<int> s1;s1.insert(40);s1.insert(10);s1.insert(20);s1.insert(60);for(set<int>::const_iterator it = s1.begin();it!=s1.end();it++){cout << *it << " ";}cout << endl;//指定排序规则set<int,person> s2;s2.insert(40);s2.insert(10);s2.insert(20);s2.insert(60);for(set<int,person>::const_iterator it = s2.begin();it!=s2.end();it++){cout << *it << " ";}cout << endl;return 0;
}
编译运行
set存放自定义数据类型
对于自定义类型,set必须指定排序规则才能插入元素
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
using namespace std;class person
{
public:person(string name,int age){this->age = age;this->name = name;}int age;string name;
};class compare
{
public:bool operator()(const person &p1,const person &p2){//年龄降序return p1.age > p2.age;}
};void test01()
{person p1("张三",20);person p2("李四",28);person p3("王五",17);set<person,compare> s;s.insert(p1);s.insert(p2);s.insert(p3);for(set<person,compare>::const_iterator it = s.begin();it!=s.end();it++){cout <<"姓名: " << it->name<< "年龄: "<<it->age;}cout << endl;
}int main()
{test01();return 0;
}
pair队组
pair<type,type>p ( value1,value2 );
pair<type,type>p = make pair( value1,value2);
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
using namespace std;int main()
{pair<string,int>p1("tom",20);cout << "姓名: "<<p1.first<<"年龄: " <<p1.second << endl;pair<string,int>p2 = make_pair("tom",20);cout << "姓名: "<<p2.first<<"年龄: " <<p2.second << endl;return 0;
}
map容器
map容器的基本概念
简介
map中所有元素都是pair
pair中第一个元素为key (键值),起到索引作用,第二个元素为value (实值)
所有元素都会根据元素的键值自动排序
本质:
map/multimap属于关联式容器,底层结构是用二叉树实现
优点:
可以根据key值快速找到value值
map和multimap区别:
map不允许容器中有重复kev值元素
multimap允许容器中有重复key值元素
map构造和赋值
map<T1,T2> mp;//map默认构造函数
map(const map &mp);//拷贝构造函数
map& operator=(const map &mp); //重载等号操作符
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
using namespace std;void print(map<int,int>&m)
{for(map<int,int>::iterator it = m.begin();it!= m.end();it++){cout << "key: "<<it->first << "value: "<<it->second<<endl; }cout << endl;
}void test01()
{map<int,int> m;m.insert(pair<int,int>(1,10)); m.insert(pair<int,int>(3,20));m.insert(pair<int,int>(2,30));m.insert(pair<int,int>(4,40));//根据key值自动排序print(m);
}int main()
{test01();return 0;
}
map大小和交换
size();//返回容器中元素的数目
empty();//判断容器是否为空
swap(at);//交换两个集合容器
map插入和删除
insert(elem);//在容器中插入元素
clear();//清除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(beg,end);//删除区间[beg,end)的所有元素,返回下一个元素的迭代器
erase(key);//删除容器中值为key的元素
void test01()
{
map<int,int> m;//插入元素
m.insert(pair<int,int>(1,10));
m.insert(make_pair(2,20));
m.insert(map<int,int>::value_type(3,30);
m[4] = 40;//不建议插数使用 可以利用key访问到value
m.erase(m.begin());//删除迭代器所指的元素,返回下一个元素的迭代器
m.erase(m.begin(),m.end());//删除区间[beg,end)的所有元素,返回下一个元素的迭代器
m.erase();//删除容器中值为key的元素
m.clear();//清除所有元素
}
map查找和统计
find(key);查找key是否存在,若存在,返回该键的元素的迭代器:若不存在,返回set.end();
count(key);//统计计key的元素个数
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
using namespace std;void test01()
{map<int,int> m;m.insert(pair<int,int>(1,10));m.insert(pair<int,int>(2,20));m.insert(pair<int,int>(3,30));map<int,int>::iterator pos = m.find(3);//查找key为3的元素 返回该元素的迭代器if(pos != m.end()){cout <<"查到了元素 key = "<< (*pos).first << "value = "<< pos->second << endl;}
}
int main()
{test01();return 0;
}
改变map排序规则
map容器默认排序是从小到大排序 利用仿函数可以改变map容器排序规则
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
using namespace std;class person
{
public:bool operator()(int v1,int v2){return v1 > v2;}
};int main()
{//默认从小到大排序map<int,int> m;m.insert(pair<int,int>(1,10)); m.insert(pair<int,int>(3,30)); m.insert(pair<int,int>(2,20)); for(map<int,int>::const_iterator it = m.begin();it!=m.end();it++){cout << "key: "<<it->first << " ";}cout << endl;//指定排序规则map<int,int,person> m1;m1.insert(pair<int,int>(1,10)); m1.insert(pair<int,int>(3,30)); m1.insert(pair<int,int>(2,20));for(map<int,int,person>::const_iterator it = m1.begin();it!=m1.end();it++){cout << "key: "<<it->first << " ";}cout << endl;return 0;
}
相关文章:
![](https://img-blog.csdnimg.cn/ece309112f3a4853a9a7834d527bfcc6.png)
set/multiset容器、map容器
目录 set/multiset容器 set基本概念 set大小和交换 set插入和删除 查找和统计 set和multiset的区别 改变set排序规则 set存放内置数据类型 set存放自定义数据类型 pair队组 map容器 map容器的基本概念 map构造和赋值 map大小和交换 map插入和删除 map查找和统计…...
![](https://www.ngui.cc/images/no-images.jpg)
Linux系统编程——总结初识Linux(常用命令、特点、常见操作系统)
文章目录 UNIX操作系统(了解)Linux操作系统主要特征Linux和unix的区别和联系什么是操作系统常见的操作系统Ubuntu操作系统Ubuntu安装linux下的目录的类型(掌握)shell指令shell指令的格式文件操作相关指令系统相关命令网络相关命令其他命令软件安装相关的…...
![](https://www.ngui.cc/images/no-images.jpg)
Js使用ffmpeg进行视频剪辑和画面截取
ffmpeg 使用场景是需要在web端进行视频的裁剪,包括使用 在线视频url 或 本地视频文件 的裁剪,以及对视频内容的截取等功能。 前端进行视频操作可能会导致性能下降,最好通过后端使用java,c进行处理,本文的案例是备选方…...
![](https://www.ngui.cc/images/no-images.jpg)
Linux基本命令,基础知识
进到当前用户目录:cd ~ 回到上级目录:cd .. 查看当前目录层级:pwd 创建目录:mkdir mkdir ruanjian4/linux/zqm41 -p级联创建文件夹(同时创建多个文件夹需要加-p) 查看详细信息:ls -l (即 ll) 查看所有详细信息:ls -al 隐藏文件是以.开头的 查看:l…...
![](https://img-blog.csdnimg.cn/c4419d29224a4a5d89d5d67e2cc7adee.png)
【Android知识笔记】进程通信(三)
在上一篇探索Binder通信原理时,提到了内存映射的概念,其核心是通过mmap函数,将一块 Linux 内核缓存区映射到一块物理内存(匿名文件),这块物理内存其实是作为Binder开辟的数据接收缓存区。这里有两个概念,需要理解清楚,那就是操作系统中的虚拟内存和物理内存,理解了这两…...
![](https://img-blog.csdnimg.cn/f9ac09c83c204c879e780adfb5fe8e42.jpeg)
云上亚运:所使用的高新技术,你知道吗?
作者简介:一名云计算网络运维人员、每天分享网络与运维的技术与干货。 公众号:网络豆云计算学堂 座右铭:低头赶路,敬事如仪 个人主页: 网络豆的主页 目录 前言 一.什么是云上亚运会 二.为什么要使用云…...
![](https://www.ngui.cc/images/no-images.jpg)
数据结构简述,时间、空间复杂度,学习网站推荐
目录 IT 学习路线 相关坚韧大厚书 相关有趣/耐看书或视频 数据结构与算法学习网站推荐 刷题 时间、空间复杂度 数据结构简述 基本概念 数据结构与算法简述和CS综述整理。本文非基础的教程,本文会列出大量学习和参考网站。老惯例,一个文章是一个集…...
![](https://img-blog.csdnimg.cn/64d16f8ab2d0479487db576704bcf802.png)
在线安装qt5.15之后任意版本
下载qt现在安装包: window安装包链接 进入cmd,用命令行打开安装包,并指定组件下载地址(这个是关键,之前用的是腾讯镜像,出现了版本灰色无法选中问题) .\qt-unified-windows-x64-4.6.1-online…...
![](https://img-blog.csdnimg.cn/97da92f9982b488480f143ec5ec36e28.png)
【kafka实战】01 3分钟在Linux上安装kafka
本节采用docker安装Kafka。采用的是bitnami的镜像。Bitnami是一个提供各种流行应用的Docker镜像和软件包的公司。采用docker的方式3分钟就可以把我们想安装的程序运行起来,不得不说真的很方便啊,好了,开搞。使用前提:Linux虚拟机&…...
![](https://www.ngui.cc/images/no-images.jpg)
yum安装mysql8
记录一下安装过程用于后面项目参考 目录 说明安装步骤yum安装默认目录修改默认的数据目录必要的my.cnf属性修改卸载Mysql 说明 一般情况下都是docker安装,部分特殊情况下,例如老外的项目部分禁用docker,那一般二进制安装或者yum直接安装。 …...
![](https://www.ngui.cc/images/no-images.jpg)
十五)Stable Diffusion使用教程:另一个线稿出3D例子
案例:黄金首饰出图 1)线稿,可以进行色阶加深,不易丢失细节; 2)文生图,精确材质、光泽、工艺(抛光、拉丝等)、形状(包括深度等,比如镂空)和渲染方式(3D、素描、线稿等)提示词,负面提示词; 3)seed调-1,让ai随机出图; 4)开启controlnet,上传线稿图,选择cann…...
![](https://www.ngui.cc/images/no-images.jpg)
2023icpc网络预选赛I. Pa?sWorD(dp)
题目给定字符串长度n以及字符串s 其中出现小写字母可以代表小写字母和大写字母 比如a可以代表a和A 出现?可以代表26个小写字母和26个大写字母和10个数字 出现大写字母和数字就是原本的数 同时要求大写字母,小写字母,数字一定都存在替换完的字符串中…...
![](https://img-blog.csdnimg.cn/17483394b4564f0281567dffe3cc065f.png#pic_center)
maven本地安装jar包
在实际开发中,有些jar包不能通过公共库下载,只能本地安装。可以按照以下步骤操作: 1、安装命令 mvn install:install-file -DgroupIdcom.chinacreator.sm -DartifactIdfbm-sm-common -Dversion0.0.1 -Dpackagingjar -Dfile../newJar/fbm-sm…...
![](https://www.ngui.cc/images/no-images.jpg)
QT中的inherits
目录 简介: 实例: 简介: 在Qt中,可以使用inherits函数来判断一个对象是否属于某个类或其派生类。inherits函数是QObject类的成员函数,因此只能用于继承自QObject的类的对象。 以下是inherits函数的一般用法…...
![](https://img-blog.csdnimg.cn/70a775d4b47446f3af099f4c2d344f1a.png)
全国职业技能大赛云计算--高职组赛题卷①(容器云)
全国职业技能大赛云计算--高职组赛题卷①(容器云) 第二场次题目:容器云平台部署与运维任务1 Docker CE及私有仓库安装任务(5分)任务2 基于容器的web应用系统部署任务(15分)任务3 基于容器的持续…...
![](https://img-blog.csdnimg.cn/06e5055b4aa247b4a50cf640eddb734a.png)
基于springboot+vue的入校申报审批系统
博主主页:猫头鹰源码 博主简介:Java领域优质创作者、CSDN博客专家、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战 主要内容:毕业设计(Javaweb项目|小程序等)、简历模板、学习资料、面试题库、技术咨询 文末联系获取 项目介绍…...
![](https://img-blog.csdnimg.cn/0753a5bcc2924a0fb0398e6337c5a387.jpeg)
安卓逆向 - EdXposed LSPosed VirtualXposed
一、引言 接上篇:安卓逆向 - Xposed入门教程_小馒头yy的博客-CSDN博客 我们介绍了Xposed入门安装使用,但是只支持到Android 8,并且安装模块需要重启。今天我们来看看Xposed的其他版本。 二、各种Xposed框架对比 1、Xposed 只支持到安卓8&…...
![](https://img-blog.csdnimg.cn/c35fcf25899f4d1bba3ed5388bb07722.png)
Linux三大搜索指令的区别
find:可以在指定的路径下进行文件的搜索 —— 真的在磁盘文件中查找 例如find /usr/bin/ -name ls which 可以在指令路径下,/usr/bin,搜索指令文件 例如:which ls whereis:在系统特定的路径下查找,既可以找到可执行程序ÿ…...
![](https://img-blog.csdnimg.cn/fa1dd3eb2b3840c690facfaa2e1ef6d6.png)
C++ -- 特殊类设计
目录 设计一个类,不能被拷贝 C98的做法 C11的做法 设计一个类,只能在堆上创建对象 实现方式1 实现方式2 设计一个类,只能在栈上创建对象 实现方式1 方式1的优化 实现方式2 设计一个类,不能被继承 设计模式 什么是设计…...
![](https://img-blog.csdnimg.cn/5cf9efcf177d41de9f5b72a141c0fc3d.jpeg)
指针和数组笔试题的透析
指针---进阶篇(三) 一、前言二、一维数组例题透析:三、指针笔试题1.例一:2.例二:3.例三:4.例四:5.例五:6.例六: 一、前言 那么好了好了,宝子们,从…...
![](https://img-blog.csdnimg.cn/c43225a23880429286d0da2e846e8274.gif#pic_center)
「UG/NX」Block UI 超级点SuperPoint
✨博客主页何曾参静谧的博客📌文章专栏「UG/NX」BlockUI集合📚全部专栏「UG/NX」NX二次开发「UG/NX」BlockUI集合「VS」Visual Studio「QT」QT5程序设计「C/C+&#...
![](https://www.ngui.cc/images/no-images.jpg)
Linux——kafka常用命令
一、Kafka的常用命令包括: 1. 启动Zookeeper服务 前台启动: ./bin/zookeeper-server-start.sh config/zookeeper.properties 后台启动: ./bin/zookeeper-server-start.sh -daemon config/zookeeper.properties 2. 停止Zookeeper服务 .…...
![](https://img-blog.csdnimg.cn/img_convert/7662998e1b2db3053ce703c00ec8fa0d.gif)
GLTF编辑器如何快速重置模型原点
1、什么是模型原点? 模型原点是三维建模中的概念,它是指在一个虚拟三维空间中确定的参考点。模型原点通常位于模型的几何中心或基本组件的中心位置。如图所示: 可以看到模型的原点在模型的几何中心 2、模型原点的作用 知道了什么是模型原点&…...
![](https://img-blog.csdnimg.cn/db2aaf039c194ff69b47779c0cdd49ad.png)
【STL】vector常见用法及模拟实现(附源码)
目录 前言1. vector介绍及使用1.1vector的介绍1.2 vector的使用1.2.1 构造函数 1.2.2 vector对象遍历1.2.3 reserve和resize1.2.4 insert和erase 2. vector模拟实现2.1 vector迭代器失效问题2.2 模拟实现reserve函数浅拷贝问题2.3模拟实现源码2.3.1 vector.h2.3.2 test.cpp 前言…...
![](https://www.ngui.cc/images/no-images.jpg)
深度学习保姆级教学
文章目录 前言1.深度学习概论2.神经网络1.基础原理2.损失函数3.SoftMax4.前向传播5.反向传播1.反向传播介绍 6 卷积神经网络应用1.检测任务2.超分辨率重构3.医学检测4.无人驾驶5. 人脸识别 6.卷积网络和传统区别7.卷积神经网络1.卷积做了什么?2.节点网络1.Alexnet2.…...
![](https://www.ngui.cc/images/no-images.jpg)
计算机视觉的优势和挑战
计算机视觉(CV)是一项快速发展的技术,它具有许多优势和挑战。以下是一些可能的例子: 优势: 1. 自动化:CV技术可以自动化任务,例如图像分类、目标检测和跟踪,从而提高生产力和减少人…...
![](https://img-blog.csdnimg.cn/img_convert/8f9ac24f7314580e4e12449aa9ea3cb6.png)
群晖管家+内网穿透实现公网远程访问本地黑群晖
白嫖怪狂喜!黑群晖也能使用群晖管家啦! 文章目录 白嫖怪狂喜!黑群晖也能使用群晖管家啦!1.使用环境要求:2.下载安装群晖管家app3.随机地址登陆群晖管家app4.固定地址登陆群晖管家app 自己组装nas的白嫖怪们虽然也可以通…...
![](https://www.ngui.cc/images/no-images.jpg)
Essential C++【读书笔记 思考总结】
本篇博客是学习过程中的笔记、思考和总结。原文链接: 3 泛型编程风格 Generic Programming3.1 指针的算术运算3.2 了解 Iterator(泛型指针)3.3 所有容器的共通操作 3 泛型编程风格 Generic Programming STL的主要组件:Container&…...
![](https://img-blog.csdnimg.cn/46b6fcb2fa604c86ae2a666c14d9072a.png)
深度学习实战基础案例——卷积神经网络(CNN)基于Xception的猫狗识别|第2例
文章目录 一、环境准备二、数据预处理三、构建模型四、实例化模型五、训练模型5.1 构建训练函数5.2 构建测试函数5.3 开始正式训练 六、可视化精度和损失七、个体预测总结 今天使用轻量级的一个网络Xception做一个简单的猫狗识别案例,我的环境具体如下: …...
![](https://www.ngui.cc/images/no-images.jpg)
Linux Systemd 配置开机自启
博文目录 文章目录 Systemd操作方式配置方式配置示例参考 Systemd Systemd 是一个用于启动、管理和监控 Linux 系统的初始化系统。它是许多现代 Linux 发行版中默认的初始化系统,取代了传统的 SysVinit 和 Upstart。 Systemd 的引入在 Linux 社区引起了一些争议&…...
![](/images/no-images.jpg)
wordpress 大学 主题/邮件营销
本文链接: https://blog.csdn.net/xietansheng/article/details/87799327 0. aapt 简介 aapt(Android Asset Packaging Tool)是 Android 资源打包工具。aapt 的主要作用是吧 Android 的各类资源(图片、布局文件、源码等)经过处理…...
![](/images/no-images.jpg)
找人做网站价格/seo科技网
在一个运行超过半年的测试结果分析程序中,经理提出了一个新的要求,需要得到每一次单元测试运行的结果趋势图,以framework为类别显示是成功还是失败。当时的数据库中其中一个大表已经还有超过600万行记录,可以预计在接下来的时间中,…...
![](https://img-blog.csdnimg.cn/90df223067a24683a3d4a6bb3ecc331b.png#pic_center)
wordpress 房产模板/cms
七.CephFS 使用 ceph FS 即ceph filesystem,可以实现文件系统共享功能,客户端通过ceph 协议挂载并使用ceph 集群作为数据存储服务器。 Ceph FS 需要运行Meta Data Services(MDS)服务,其守护进程为ceph-mds,ceph-mds 进程管理与cephFS 上存储的文件相关的元数据,并协调对c…...
![](/images/no-images.jpg)
wordpress增加访问速度/网站排名
搬寝室 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 20642 Accepted Submission(s): 7013 Problem Description 搬寝室是非常累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3号楼,…...
![](/images/no-images.jpg)
网站改版新闻/国家职业技能培训平台
QString 类是 Qt 中用于表示字符串的类,实现在 QtCore 共享库中。QString 类在实现上有以下特征。 1)字符串采用 Unicode 内部编码,可以表示世界上大多数语言的文字。 2)字符串的存储有引用计数,当一个 QString 对象被…...
![](/images/no-images.jpg)
最火的二十个电商app/广州网络推广seo
这里总结了常见的一些mysql错误,会不断更新。 要求大家将如下错误的每个单词都知道是什么意思,方便调错。 --1.语法错误:SQL syntax [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL …...