首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >STL_set/multiset

STL_set/multiset

作者头像
GeekLiHua
发布于 2025-01-21 05:52:17
发布于 2025-01-21 05:52:17
10100
代码可运行
举报
文章被收录于专栏:JavaJava
运行总次数:0
代码可运行

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的构造方法

学习代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#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大小和交换

学习代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#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);

	// Print
	cout << "s1: ";
	PrintSet(s1);
	cout << endl;

	// Judge empty 
	if (s1.empty())
	{
		cout << "s1为空" << endl;
	}
	else
	{
		cout << "s1不为空" << endl << "s1的大小为:" << s1.size() << endl;
	}

	// Swap
	set<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 --;

学习代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#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 30
	PrintSet(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 30
	cout << "s1: " << endl;
	PrintSet(s1);
	cout << endl;

	s1.erase(20);  // delete by value 按值删除元素 
	// 10 30
	cout << "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,

学习代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#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 insert

	pair<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 element

	ms.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. 运行结果

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#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 insert
	s1.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 large
	cout << 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 10
	for (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;
}

运行结果

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-10-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
C++中STL-set详解
目录 set/ multiset容器 1. set基本概念 2.set构造和赋值 3.set大小和交换 4.set插入和删除 5.set容器-查找和统计 6.set和multiset的区别 7.pair对组创建 8.set容器排序 9.set存放自定义数据类型 ---- set/ multiset容器 1. set基本概念 简介: 所有元素都会在插入时自动被排序 本质: set/multiset属于关联式容器,底层结构是用二叉树实现。 set和multiset区别:  set不允许容器中有重复的
莫浅子
2022/12/09
4800
C++中STL-set详解
c++STL容器之set/multiset容器
1.所有元素在插入时就会被自动排序。 2.底层是二叉树的实现。 3.set中不允许有重复的元素,multiset里面允许有重复的元素。 一、构造函数 set<T> st; set(const set &st); 二、赋值 set& operator(const set &st); 三、大小和交换 size(); empty(); swap(); 四、插入和删除 insert(ele); clear(); erase(pos); erase(beg,end); erase(ele); 五、查找和统计 find
西西嘛呦
2020/08/26
3790
⭐️STL⭐️之list,set,map全解,❤️算法必备❤️<下>
文章目录 😘 闲聊几句 👍 list 👍list的反转和排序 👍set/multiset 👍对组 👍map / multimap ❤️最后 😘 闲聊几句 时间过的很快,码神马上就要开学了,这也是STL系列的最后一篇了,假期学了不少,距离自己的奥赛巅峰水平可以说是十分接近了,如果说学这c++有什么用的话,可能就是兴趣所至吧,在博客更新之际,也认识了不少行业大佬,给我提了不少意见,感谢!STL完了以后,就是算法和python脚本吧,做自己想做的事情,更要做难的事情,总体来说STL的浏览量不多,但是还要说,why
秋名山码神
2022/12/13
2860
⭐️STL⭐️之list,set,map全解,❤️算法必备❤️<下>
C++进阶
建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来代表。
全栈程序员站长
2022/07/13
6090
❤ 挑战C站最强C++ STL标准库总结(内含大量示例)
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家,(ノ´▽`)ノ♪-》点击这里->一个宝藏级人工智能教程网站。
全栈程序员站长
2022/09/09
1.5K0
❤ 挑战C站最强C++ STL标准库总结(内含大量示例)
【C++】set和multiset的常用接口详解
前⾯我们已经接触过STL中的部分容器如:string、vector、list、deque、array、forward_list等,本篇文章将介绍一下map和multiset的使用。
羚羊角
2025/06/02
1310
【C++】set和multiset的常用接口详解
set容器和multiset容器的区别
区别: #include<iostream> using namespace std; #include<set> void p(const set<int>& s) { for (set<int>
大忽悠爱学习
2021/03/02
5300
set容器和multiset容器的区别
set容器排序
内置类型指定排序规则 主要技术点:利用仿函数,可以改变排序规则 #include<iostream> using namespace std; #include<set> class myCompare { public: //利用仿函数,重载() bool operator()(int a, int b)const { //降序 return a > b; } }; void p(const set<int>& s) { for (set<int>::const_iterator it
大忽悠爱学习
2021/03/02
3370
set容器排序
STL set
STL 对这个序列可以进行查找,插入删除序列中的任意一个元素,而完成这些操作的时间同这个序列中元素个数的对数成比例关系,并且当游标指向一个已删除的元素时,删除操作无效。而一个经过更正的和更加实际的定义应该是:一个集合(set)是一个容器,它其中所包含的元素的值是唯一的。这在收集一个数据的具体值的时候是有用的。集合中的元素按一定的顺序排列,并被作为集合中的实例。一个集合通过一个链表来组织,在插入操作和删除操作上比向量(vector)快,但查找或添加末尾的元素时会有些慢。具体实现采用了红黑树的平衡二叉树的数据结构。
十四君
2019/11/27
6890
C++ —— set系列的使用
https://legacy.cplusplus.com/reference/set/
迷迭所归处
2024/11/19
1430
C++ —— set系列的使用
【c++丨STL】set/multiset的使用
之前,我们已经探索了STL中的多个容器及容器适配器,如string、vector、list,以及stack和priority_queue等。今天,我们将进一步拓宽视野,深入学习STL中的容器——set及其变种multiset。
ephemerals__
2024/12/25
2080
【c++丨STL】set/multiset的使用
C++提高编程笔记合集
建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来代表。
CtrlX
2022/10/27
1K0
C++提高编程笔记合集
C++ STL学习之容器set和multiset (补充材料)
一、set和multiset基础 set和multiset会根据特定的排序准则,自动将元素进行排序。不同的是后者允许元素重复而前者不允许。 需要包含头文件: #include <set> set和mu
Angel_Kitty
2018/04/08
1.2K0
C++ STL学习之容器set和multiset (补充材料)
C++:set和map的使用
一般来说,像string、vector、list、deque、forward_list等容器,这些容器的底层逻辑机构为线性序列的数据结构,所以这些容器也叫做序列式容器,序列式容器两个位置存储的值之间一般没有紧密的关联关系,如若将其交换,依旧是序列式容器。序列式容器中的元素是按他们在容器中的存储位置保存和访问的。
HZzzzzLu
2024/11/26
2420
C++:set和map的使用
STL(一)vector、set/multiset、listVectorSetmultisetlist
vector封装数组,list封装了链表,map和set封装了二叉树等。set关联式容器。set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据,在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序。应该注意的是set中数元素的值不能直接被改变。C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Red-Black Tree)。RB树的统计性能要好于一般平衡二叉树,所以被STL选择作为了关联容器的内部结构。set插入是按照一定规则排序,默认是由小到大。
用户2929716
2018/08/23
4710
STL(一)vector、set/multiset、listVectorSetmultisetlist
STL学习笔记(9)常用容器 set/multiset
Set 的特性是:所有元素都会根据元素的键值自动被排序。Set 的元素不像 map 那样可以同时拥有实值和键值,set 的元素即是键值又是实值。Set 不允许两个元素有相同的键值。 我们可以通过 set 的迭代器改变 set 元素的值吗?不行,因为 set 元素值就是其键值,关系到 set 元素的排序规则。 如果任意改变 set 元素值,会严重破坏 set 组织。换句话说,set 的 iterator 是一种 const_iterator. set 拥有和 list 某些相同的性质,当对容器中的元素进行插入操作或者删除操作的时候,操作之前所有的迭代器, 在操作完成之后依然有效,被删除的那个元素的迭代器必然是一个例外。
轻舞飞扬SR
2021/04/13
3220
STL学习笔记(9)常用容器 set/multiset
set容器之构造和赋值
set容器概念:所有容器在被插入时都会被自动排序 本质:set和multiset属于关联式容器,底层结构是用二叉树实现 set和multiset容器的区别:
大忽悠爱学习
2021/03/02
6400
set容器之构造和赋值
set容器的插入与删除
插入与删除 函数原型: #include<iostream> using namespace std; #include<set> void p(set<int>& s) { for (set<
大忽悠爱学习
2021/03/02
3990
set容器的插入与删除
【C++】STL标准模板库容器set
multiset的接口是和set一模一样的,区别在于具体的使用上:
修修修也
2024/09/28
1620
【C++】STL标准模板库容器set
深入剖析C++ STL中的set:高效管理有序数据的利器
什么是 set? set 是 C++ STL 提供的一个模板类,基于红黑树实现,具有以下核心特性: 元素唯一:set 会自动去重,插入相同的元素时,新元素会被忽略。 自动排序:默认情况下,set 按照升序排列元素,也可以通过自定义比较器实现自定义排序。 高效操作:常见操作如插入、删除、查找的时间复杂度为 𝑂(log𝑛)。
用户11286421
2024/11/21
2530
深入剖析C++ STL中的set:高效管理有序数据的利器
相关推荐
C++中STL-set详解
更多 >
LV.3
腾讯业务安全工程师
作者相关精选
换一批
交个朋友
加入腾讯云官网粉丝站
蹲全网底价单品 享第一手活动信息
加入讨论
的问答专区 >
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档