Introduction to c++ set usage must see super detailed

1. The role of set
Set means set, and the feature of set is that there will be no duplicate content. It is generally used for duplication checking or deduplication operations. For example, a table is given:

Name Hobby
Xiao Ming play basketball
Xiao Gang draw
Xiao Ming listen to music
Ask how many people appear in the table, learn set, you can easily solve this problem

2. The definition of set
set<stored type> container name
such as:
store int type value set<int> s;
store double type value set<double> s;
store string type value set<string> s;
store structure The value of the body or class value set<structure name> s;

(1) Some basic member functions of set

//常用函数(必学)
insert()//插入元素
count()//判断容器中是否存在某个元素
size()//返回容器的尺寸,也可以元素的个数
erase()//删除集合中某个元素
clear()//清空集合
empty()//判断是否为空
begin()//返回第一个节点的迭代器
end()//返回最后一个节点加1的迭代器
rbegin()//反向迭代器
rend()//反向迭代器

//功能函数(进阶)
find()//查找某个指定元素的迭代器
lower_bound()//二分查找第一个不小于某个值的元素的迭代器
get_allocator()//返回集合的分配器
swap()//交换两个集合的变量
max_size()//返回集合能容纳元素的最大限值

code:

#include<iostream>//c++标准头文件,可以使用cout,cin等标准编译用法
#include<set>//使用set需要带上这个文件
using namespace std;//命名空间,防止重名给程序带来各种隐患,使用cin,cout,map,set,vector,queue时都要使用
int main(){
    
    
	set<int> s;//定义 
	s.insert(1);//插入元素1 
	s.insert(3);//插入元素3
	s.insert(2);//插入元素2
	cout<<"现有的元素有"<<endl; 
	for(int c:s){
    
    //遍历set,注意set会将元素自动排序,插入的顺序是1、3、2,遍历的顺序是1、2、3 
		cout<<c<<' ';
	} 
	cout<<endl;
	cout<<endl;
	s.erase(3);//删除元素3
	
	cout<<"删除元素3后,现有的元素有"<<endl; 
	for(int c:s){
    
    //遍历set,注意set会将元素自动排序,插入的顺序是1、3、2,遍历的顺序是1、2、3 
		cout<<c<<' ';
	} 
	cout<<endl;
	cout<<endl;
	
	cout<<"现在s.size()=="; 
	cout<<s.size();
	cout<<",即有两个元素" ;
	cout<<endl;
	cout<<endl;
	
	cout<<"是否包含元素2:"<<endl;
	cout<<"s.count(2)=="<<s.count(2)<<"即包含元素2"; 
	cout<<endl;
	cout<<endl;
	cout<<"是否包含元素3:"<<endl;
	cout<<"s.count(3)=="<<s.count(3)<<"即不包含元素3"; 
	cout<<endl;
	cout<<endl;
	
	cout<<"s是否是空的:"<<endl;
	cout<<"s.empty()=="<<s.empty()<<"即s不为空"; 
	cout<<endl;
	cout<<endl;
	
	s.clear();//清空集合 
	
	cout<<"s是否是空的:"<<endl;
	cout<<"s.empty()=="<<s.empty()<<"即s是空的"; 
	cout<<endl;
	cout<<endl;
	
	
	cout<<"s是否是空的:"<<endl;
	cout<<"s.size()=="<<s.size()<<"即s是空的"; //s.size()==0也可以判断集合是否为空,为了考虑代码可读性,一般不用size()代替empty() 
	cout<<endl;
	cout<<endl;
} 

operation result:

现有的元素有
1 2 3

删除元素3后,现有的元素有
1 2

现在s.size()==2,即有两个元素

是否包含元素2:
s.count(2)==1即包含元素2

是否包含元素3:
s.count(3)==0即不包含元素3

s是否是空的:
s.empty()==0即s不为空

s是否是空的:
s.empty()==1即s是空的

s是否是空的:
s.size()==0即s是空的

3. Two traversal methods of set

(1) Iterator iterator
code:

#include<iostream>
#include<set>
using namespace std;
int main(){
    
    
	set<int> s;//定义 
	s.insert(1);//插入元素1 
	s.insert(3);//插入元素3
	s.insert(2);//插入元素2
	set<int>::iterator it;//使用迭代器
	for(it=s.begin();it!=s.end();it++){
    
    
		cout<<*it<<' ';
	} 
} 

operation result:

1 2 3

Set has a very important feature, that is, automatic ascending sorting, which can be conveniently used in many scenarios, so what should be done when descending sorting is required?
1. Reverse thinking Traversing
from end()-1 to begin() is in descending order

#include<iostream>
#include<set>
using namespace std;
int main(){
    
    
	set<int> s;//定义 
	s.insert(1);//插入元素1 
	s.insert(3);//插入元素3
	s.insert(2);//插入元素2
	set<int>::iterator it;//使用迭代器
	for(it=--s.end();it!=--s.begin();it--){
    
    
		cout<<*it<<' ';
	} 
} 

operation result:

3 2 1

2.
The reverse iterators of rbegin() and rend() are originally used to realize the function of reverse iteration. Let’s see the usage below.

#include<iostream>
#include<set>
using namespace std;
int main(){
    
    
	set<int> s;//定义 
	s.insert(1);//插入元素1 
	s.insert(3);//插入元素3
	s.insert(2);//插入元素2
	set<int>::reverse_iterator it;//使用反向迭代器
	for(it=s.rbegin();it!=s.rend();it++){
    
    
		cout<<*it<<' ';
	} 
} 

operation result:

3 2 1

(2) foreach traversal

#include<iostream>
#include<set>
using namespace std;
int main(){
    
    
	set<int> s;//定义 
	s.insert(1);//插入元素1 
	s.insert(3);//插入元素3
	s.insert(2);//插入元素2
	for(auto it:s){
    
    
		cout<<it<<' ';
	} 
} 

operation result:

1 2 3

This way of writing is simple and easy to remember, but it cannot implement descending traversal

Extra-curricular knowledge
auto usage, c++ auto usage is powerful, when you can not determine the type of variable, you can use auto instead, the iterator iterator is difficult to remember, in fact, you can use auto instead:

#include<iostream>
#include<set>
using namespace std;
int main(){
    
    
	set<int> s;//定义 
	s.insert(1);//插入元素1 
	s.insert(3);//插入元素3
	s.insert(2);//插入元素2
	for(auto it=s.begin();it!=s.end();it++){
    
    
		cout<<*it<<' ';
	} 
} 

operation result:

1 2 3

Is it very simple?

It will definitely be difficult for newcomers. Do more questions and use them more. Once you get familiar with it, it will be easy. Brother Meng, come on! ! !

There are still deficiencies in the article, welcome to correct me

Thanks for watching, like it

Guess you like

Origin blog.csdn.net/weixin_52115456/article/details/124210086