C++_STL_set
#include<iostream>
//导入set头文件
#include<set>
using namespace std;
int main(){
//声明
set<int> s;
//插入元素
s.insert(1);
s.insert(2);
s.insert(3);
//元素个数
cout<<"元素个数:"<<s.size()<<endl;
//查找元素
set<int>::iterator ite;
ite = s.find(2);
if(ite==s.end())
cout<<"Not Found"<<endl;
else
cout<<"Found it:"<<*ite<<endl;
//删除元素
s.erase(2);
ite = s.find(2);
if(ite==s.end())
cout<<"Not Found"<<endl;
else
cout<<"Found it:"<<*ite<<endl;
//其他查找元素的方法
//count():返回集合中某个值元素的个数
if(s.count(3)==0)
cout<<"Not Found"<<endl;
else
cout<<"Find it:"<<s.count(3)<<endl;
//遍历所有元素
for(ite=s.begin(); ite!=s.end(); ite++){
cout<<*ite<<" ";
}
cout<<endl;
return 0;
}

浙公网安备 33010602011771号