STL_set/multiset

article/2025/11/5 8:21:48

STL_set/multiset

简介:本文主要介绍STL中的,set与multiset的使用,只需要把本文的代码自己敲完便可学会。

set容器的基本概念

在这里插入图片描述

注意:set容器没有push_back, pop_back这两种插入接口,只能用insert函数进行插入
如果向set容器种插入相同元素,不会报错,但是打印的时候会自动滤去多余的元素,一个元素只能有一个,而且打印结果事是排好序了的。

Note:the set container has no push_back and pop_back these two kinds of insertion interface, can only use the insert function to insert.
if the same element is inserted into the set container, no error will be reported, but the redundant elements will be automatically filtered out when printing. There can only be one element, and the printing results are in good order.

set的构造方法

在这里插入图片描述
学习代码

#include<iostream>
#include<set>using namespace std;
/*Construction and assignment of set container*/
// 遍历set的函数
void PrintSet(const set<int>& s)
{for (set<int>::const_iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}
void test01()
{set<int>s1;// Insert data only the way to insert data// 通过insert方法给set插入元素s1.insert(10);s1.insert(0);s1.insert(30);s1.insert(30);s1.insert(20);// Traverse container// 遍历容器cout << "s1: " << endl;  // Automaticallu order and remove duplicate elements// 0 10 20 30             PrintSet(s1);cout << endl;// Copy constructor// 拷贝函数来构造set<int>s2(s1);cout << "s2: " << endl;// 0 10 20 30            PrintSet(s2);cout << endl;// Assgin // 也是拷贝构造函数,用=运算符实现set<int>s3;s3 = s1;cout << "s3: " << endl;// 0 10 20 30            PrintSet(s3);cout << endl;
}
int main()
{test01();return 0;
}

运行结果
在这里插入图片描述

set大小和交换

在这里插入图片描述
学习代码

#include<iostream>
#include<set>using namespace std;/*Size container, size and swap*/void PrintSet(const set<int>& s)
{for (set<int>::const_iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{set<int>s1;// Data insert// 插入数据s1.insert(10);s1.insert(20);s1.insert(30);s1.insert(40);// Printcout << "s1: ";PrintSet(s1);cout << endl;// Judge empty if (s1.empty()){cout << "s1为空" << endl;}else{cout << "s1不为空" << endl << "s1的大小为:" << s1.size() << endl;}// Swapset<int>s2;s2.insert(12);s2.insert(80);s2.insert(132);s2.insert(567);cout << "s1:" << endl;PrintSet(s1);cout << endl;cout << "-----------------\n" << endl;cout << "交换操作\n" << endl;cout << "交换前:" << endl;cout << "s1:";PrintSet(s1);cout << endl;cout << "s2:";PrintSet(s2);cout << endl;cout << "交换后:" << endl;// 交换set容器中的数据s1.swap(s2);cout << "s1:";PrintSet(s1);cout << endl;cout << "s2:";PrintSet(s2);cout << endl;
}int main()
{test01();return 0;
}

运行结果
在这里插入图片描述

set插入和删除

在这里插入图片描述
注意:set内部的底层原理是树状结构,所以和链表一样,迭代器都不支持像“+ n”, “- n”之类的直接的更改位置,原因和链表list相似,要改的话也只能用++或–。
Note:the underlying principle of set is tree structure, so like linked lists, iterators do not support direct location changes such as “+ n”, “- n”.The reason is similar to linked list to linked list, so you can only use ++ or --;

学习代码

#include<iostream>
#include<set>using namespace std;/*Set container, Insertion and deletion*/void PrintSet(const set<int>& s)
{for (set<int>::const_iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{set<int>s1;// Data insert// 插入数据 s1.insert(10);s1.insert(30);s1.insert(20);s1.insert(4);// Traverse// 遍历 cout << "s1: " << endl;// 4 10 20 30PrintSet(s1);cout << endl;// Deletion// 删除第一个元素 s1.erase(s1.begin());  // Find the location of deletion through iterator // Operators other than ++ and -- operator are not supported// 10 20 30cout << "s1: " << endl;PrintSet(s1);cout << endl;s1.erase(20);  // delete by value 按值删除元素 // 10 30cout << "s1: " << endl;PrintSet(s1);  cout << endl;// clear s1.erase(s1.begin(), s1.end()); // Through iterator 删除一个区间 cout << "s1: " << endl;PrintSet(s1);cout << endl;// The two ways are the same	s1.clear();cout << "s1: " << endl;PrintSet(s1);cout << endl;
}int main()
{test01();return 0;
}

运行结果
在这里插入图片描述

set查找和统计

在这里插入图片描述
注意:由于set函数具有元素不重复的特点,所以count函数的返回值要么是0,要么是1,multiset允许重复值,所以返回值可能不一样,利用这个性质我们也可以通过count函数来判断该元素是否存在于这个容器中。
end()迭代器表示的是最后一个元素的下一个位置,不是最后一个元素的位置。

Note:because the set function has the feature of non repeating elements, the return value of the return value of the count function is either 0 or 1. multiset allows repeating values, so the return value may be different. Using this property,we can also use the count function to determine whether the element exists in this container.
The end() iterator represents the next position of the last element, not the position of the last element,
运行结果
在这里插入图片描述

set和multiset区别

在这里插入图片描述
注意:这里引入的对组pair概念后面会简单的讲解,这里使用对组pair的目的是检验insert函数是否插入成功,验证set不能存放两个相同的元素。
multiset相对set除了可以插入重复的值以外其他的都是相似的。

Note: the concept of pair introduced here will be explained briefly later, The purpose of using pair here is to check whether the insert function is inserted successfully and verify that set cannot store two identical elements.
Multiset is similar to set except that it can insert duplicate values,

学习代码

#include<iostream>
#include<set>using namespace std;/*The different between set and multiset*/void PrintSet(const set<int>& s)
{for (set<int>::const_iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}void PrintSet(const multiset<int>& s)
{for (multiset<int>::const_iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}
void test01()
{set<int>s1;// Data insertpair<set<int>::iterator, bool> ret = s1.insert(10); // multiset貌似不能够这样做if (ret.second){cout << "第一次插入成功" << endl;}else{cout << "第一次插入失败" << endl;}cout << "当前存放元素" << endl;cout << "s1: ";PrintSet(s1);cout << endl;ret = s1.insert(10);if (ret.second){cout << "第二次插入成功" << endl;}else{cout << "第二次插入失败" << endl;}cout << "当前存放元素" << endl;cout << "s1: ";PrintSet(s1);cout << endl;cout << "------------------" << endl;multiset<int>ms;// Insert repeat elementms.insert(20);ms.insert(20);cout << "ms: " << endl;PrintSet(ms);cout << endl;}int main()
{test01();cout << endl;return 0;
}

运行结果
在这里插入图片描述

set容器排序

在这里插入图片描述
注意:定义的这个类就是这个仿函数,迭代器的类型要与定义的时候的类型一样。
set容器的顺序在定义的时候就已经拍好了,不能再更改了
这个类里面的函数定义的方法记住就好了,以后会详细讲解

Note:the defined “class” is the imitation function, and the type of iterator should be the same as the defined type.
The sequence of the set container has been arranged at the time of definition and can’t be changed any more.
Just remember the method of function definition in this class,which will be explained in detail later.
运行结果

#include<iostream>
#include<set>using namespace std;/*set container sort*/class MyCompare   // This class MyCmp is an imitation function
{
public:bool operator()(int v1, int v2) const  // 后面一定要加这个const表示这个函数是常函数,不然vs2019会报错{return v1 > v2;}
};void PrintSet(const set<int>& s)
{for (set<int>::const_iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{set<int>s1;// Data inserts1.insert(10);s1.insert(30);s1.insert(40);s1.insert(50);s1.insert(20);cout << "s1: " << endl;// 10 20 30 40 50 PrintSet(s1);   // The default rule is from small to largecout << endl;// Specify rules from small to large// 自定义排序值从大到小 set<int, MyCompare>s2;s2.insert(10);s2.insert(30);s2.insert(40);s2.insert(50);s2.insert(20);cout << "s2: " << endl;// 50 40 30 20 10for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) // Different definitions have different iterators{cout << *it << " ";}cout << endl;}int main()
{test01();cout << endl;return 0;
}

运行结果
在这里插入图片描述


http://chatgpt.dhexx.cn/article/6GnPCv9K.shtml

相关文章

Column-Stores vs. Row-Stores: How Different Are They Really?

Column-Stores vs. Row-Stores: How Different Are They Really? 论文阐述的就是行存与列存 两者之间到底有什么区别 Abstract 论文首先给出结论&#xff1a;列式存储&#xff08;Column Stores&#xff09;比行式存储&#xff08;Row Stores&#xff09;在性能上好过一个数…

android:stretchcolumns=0,1,2,3,android:stretchColumns用法

TableLayout是一个使用复杂的布局&#xff0c;最简单的用法就仅仅是拖拉控件做出个界面&#xff0c;但实际上&#xff0c;会经常在代码里使用TableLayout&#xff0c;例如做出表格的效果。本文主要介绍TableLayout的基本使用方法。 < ?xml version"1.0" encoding…

cannot set a row with mismatched columns

错误&#xff1a;cannot set a row with mismatched columns 错误背景原错误情况错误原因解决方法 错误背景 在希望将dataframe a 中的特定行移至dataframe b 时出错&#xff0c;记录下自己使用的方法 原错误情况 #dataframe a 已知 a{ab:[1,2,3],bb:[3,4,5],cb:[4,5,6]} a …

TableLayout中stretchColumns、shrinkColumns的用法

android:stretchColumns"1" android:shrinkColumns"1"这两个属性是TableLayout所特有的&#xff0c;也是这两个属性影响了子对象的布局。 表格布局是按照行列来组织子视图的布局。表格布局包含一系列的Tabrow对象&#xff0c;用于定义行&#xff08;也可以…

TableLayout中stretchColumns和shrinkColumns使用

<?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"android:layout_height"match_parent"android:orientatio…

基于JAVA的即时通信软件

一.设计任务书 1.1 设计任务 本文设计的是一个简单的即时通信软件&#xff0c;利用 Java Socket 进行点到点通信&#xff0c;其工作机制模仿即时通信软件的基本功能&#xff0c;已实现的功能有&#xff1a; 客户端登录 客户端退出 群组成员之间传输文字或图片信息 该软件分为客…

一文教你用java实现即时通讯软件的设计

即时通讯软件即所谓的聊天工具&#xff0c;其主要用途是用于文字信息的传递与文件传输。使用eclipse作为即时通讯软件的开发工具&#xff0c;使用Socket建立通讯渠道&#xff0c;多线程实现多台计算机同时进行信息的传递&#xff0c;swing技术等进行实际开发相对比较合适。通过…

即时通讯软件七大优势详解

在以前&#xff0c;即时通讯软件被认为是一种专供个人使用的通信工具&#xff0c;随着社会的发展&#xff0c;各大商家企业开始慢慢接受这种通信工具并利用其协调公司内部沟通&#xff0c;从而满足公司的业务需求。那么即时通讯软件优势有哪些&#xff1f;为你细数它的七大优势…

开发IM即时通讯容易吗?需要什么技术

即时通讯现在已经随着互联网技术的应用走进了千家万户&#xff0c;跟早些年的通信工具不同&#xff0c;现在的即时通讯技术已经涵盖了语音即时通讯、视频即时通讯、文字即时通讯等多种方式&#xff0c;而开发即时通讯也成了很多互联网企业投身这一行业后想要尝试的内容。开发即…

桌面软件开发框架大赏

本文基于海康威视桌面端技术专家刘晓伦在「RTC Dev Meetup • 杭州站丨大前端时代的业务架构和跨端实践」活动中分享内容二次整理。 以下正文&#xff1a; 今天要与大家分享 19 款桌面软件开发框架&#xff0c;我将它们分了四类&#xff0c;然后分别就每个类别做相应的介绍&a…

仿QQ即时通讯软件开发-赖国荣-专题视频课程

仿QQ即时通讯软件开发—7495人已学习 课程介绍 会使用JAVA的Swing做UI&#xff0c;学会用JAVA操作数据库&#xff0c;用Java的网络编程&#xff0c;多线程编程&#xff0c;制作一个仿QQ的即时通讯软件&#xff0c;实现在局域网或者互联网通讯 课程收益 制作仿QQ即时通讯软件…

Qt制作局域网即时通讯软件

Qt制作局域网即时通讯软件 利用Qt制作的局域网即时通信软件&#xff0c;可实现文本信息、表情包、图片、文档等的传输功能。界面风格模仿的Tim&#xff0c;所以本软件取名为Timi&#xff0c;tim的mini版本。 登录界面&#xff1a;使用之前做的登录界面&#xff0c;后续修改。原…

java实现即时通讯软件

导读:即时通讯软件即所谓的聊天工具,其主要用途是用于文字信息的传递与文件传输。使用eclipse作为即时通讯软件的开发工具,使用Socket建立通讯渠道,多线程实现多台计算机同时进行信息的传递,swing技术等进行实际开发相对比较合适。通过一些轻松的注册登录后,在局域网中即…

IM即时通讯软件系统源码安卓、苹果、PC端全开源!

demo软件园每日更新资源,请看到最后就能获取你想要的: ​ 1.《计算机系统结构&#xff1a;解析思路习题》课后答案 "本书是按照全国高等教育自学考试指导委员会制定的计算机及应用专业独立本科段“计算机系统结构自学考试大纲’’要求&#xff0c;并以其指定的自学教材内…

即时通讯软件开发:如何解决网络不稳定的问题?

随着智能手机和互联网的普及&#xff0c;即时通讯软件成为人们生活中不可或缺的一部分。随着即时通讯软件的使用越来越普及&#xff0c;网络不稳定的问题也越来越严重。为了提供更好的服务&#xff0c;我们需要解决这个问题。 网络不稳定的原因 网络不稳定可能由许多因素引起…

im即时通讯软件开发:一文即懂什么是高并发

在即时通讯网社区里&#xff0c;多是做IM、消息推送、客服系统、音视频聊天这类实时通信方面的开发者&#xff0c;在涉及到即时通讯技术时聊的最多的话题就是高并发、高吞吐、海量用户。 代码还没开始写&#xff0c;就考虑万一哪天这IM用户量破百万、千万该怎么办的问题&#…

即时通讯软件源代码_提高软件开发效率的神器,马上收藏起来吧

根据用户开发水平的差异&#xff0c;力软敏捷开发框架提供多种实现业务功能的开发模式。 如果您是初级开发者或者您根本就不会编程那也不要紧&#xff0c;系统可以根据开发向导直接进行功能开发&#xff0c;不需要编写一行代码也无需编译就个完成开发过程。 如果您是一名专业的…

QT开发的即时通讯聊天软件

最近项目需求用QT开发了一款即时通讯软件&#xff0c;差不多了上几个图看看。 登陆界面 主界面-用户列表 最近消息界面 好友聊天界面 文件传输界面 查找和添加好友 消息管理器 系统设置界面 先传这么多&#xff0c;&#xff1a;&#xff09;

基于C++的即时通信软件设计

目录 — 、 概 述 1 1 . 1 设 计 目 的 &#xff1a; 1 1 . 2 设 计 内 容 &#xff1a; 1 1 . 2 . 1 功 能 设 计 &#xff1a; 1 1 . 2 . 2 界 面 设 计 &#xff1a; 1 1 . 2 . 3 客 户 端 、 服 务 器 设 计 &#xff1a; 1 1 . 3 设 计 要 求 &#xff1a; 1 二 、 设 计 …

企业即时通讯软件开发基本功能有哪些?

即时通讯是基于互联网技术的新型交流沟通方式&#xff0c;是目前最流行的通讯方式&#xff0c;广泛的应用市场使得各种各样的即时通讯软件系统也层出不穷&#xff0c;企业即时通讯就是其中的一种延伸。是一种面向企业终端使用者的网络营销、网络沟通和内容管理的工具服务&#…