0
点赞
收藏
分享

微信扫一扫

set容器使用基本操作

WikongGuan 2022-04-03 阅读 45
c++

①引入头文件

#include<set>

②定义set

set <int> s;

③set的基本操作---增删查空

s.insert(x)    #在s容器中插入元素x
s.erase(x)     #在s容器中删除元素x
s.size()       #容器s的大小
s.count(x)
s.empty()  

④set基本操作的使用和遍历

#include<set>
#include<bits/stdc++.h>
using namespace std;
int main(void){
	set<int> s;
	s.insert(1);
	s.insert(-1);
	s.insert(2);
	s.insert(5);
	s.insert(9);
	s.erase(-1);
	int it;
	for(set<int>::iterator it=s.begin();it!=s.end();it++){
		cout<<*it<<" ";
	}
	cout<<endl;
	int num=s.size();
	cout<<num<<endl;
	if(s.count(9)==1){
		cout<<"find 9"<<endl;
	}
	else{
		cout<<"not find 9"<<endl;
	}
	return 0;
}

 

举报

相关推荐

0 条评论