Set
来源:互联网 发布:手机淘宝在线客服 编辑:程序博客网 时间:2024/06/05 22:31
vector封装数组,list封装了链表,map和set封装了二叉树
cout<<"********************知识点1:普通集合set(排序):类似数组(未排序)***********************"<<endl;
/*
Set集合的特质:1.元素升序排序,不允许包含相同的元素
如:输出是1 2 3 6
2.遍历是迭代器,不能是数组方式的遍历
*/
int main(){
set <int> s;
s.insert(2);
s.insert(1);
s.insert(6);
s.insert(1);
s.insert(3);
int a=3;
//s.count()
cout<<s.count(a)<<endl;//判断元素k是否在容器内
//s.erase()
s.erase(a); //若存在,则删除元素 a 1 2 6
//s.find()
cout<<*s.find(2)<<"*"<<endl;// 寻找k,若找到返回对应的迭代器,否则返回end();
//s.size()
cout<<s.size()<<endl;// 返回容器现有元素个数
//s.empty()
cout<<s.empty()<<endl;//原来的数据为非空 否=0
//定义正向迭代器
set <int>::iterator it;
for(it=s.begin();it!=s.end();it++){
cout<<*it<<" ";
}
cout<<endl;
// 定义反向迭代器
set<int>::reverse_iterator rit;
for(rit=s.rbegin();rit!=s.rend();rit++){
cout<<*rit<<" ";
}
}
cout<<"**************************知识点2:结构体set******************************"<<endl;
struct Stu
{
string name;
double score;
bool operator < (const Stu &another) const // 重载“<”操作符,自定义排序规则
{
if(score<another.score ) return false;
//当分数相同的时候,名字降序排序
//不添加该语句的时候,类似栈,后进先出
if(score==another.score&&name<another.name) return false;
return true;
//以上两个语句等价于:return another.score < score;
//按score由大到小排序。如果要由小到大排序,使用“>”即可。
}
};
int main()
{
set<Stu> s;
Stu Student;
/*方法1:插入元素
Student.name = "张三";
Student.score = 80;
s.insert(Student);
Student.name = "李啊";
Student.score = 99;
s.insert(Student);
Student.name = "李四";
Student.score = 99;
s.insert(Student);
Student.name = "李二狗";
Student.score = 99;
s.insert(Student);
Student.name = "李六";
Student.score = 99;
s.insert(Student);
Student.name = "王五";
Student.score = 60;
s.insert(Student);
*/
//方法2:插入元素
int n=3;
bool flag=false;
set<Stu>::iterator it;
cout<<"开始录入:"<<endl;
while(n){
cin>>Student.name>>Student.score;
if(s.empty()==1){//空,进行插入
s.insert(Student);
}
else{
for(it = s.begin(); it != s.end(); it++){
//判断当前集合内是否存在,发现不存在 进行插入
if((*it).name!=Student.name){
flag=true;//不存在
}
}
}
if(flag)s.insert(Student);
n--;
flag=false;
}
cout<<"结果展示:"<<endl;
for(it = s.begin(); it != s.end(); it++)
cout << (*it).name << " : " << (*it).score << endl;
//删除元素 zhao辍学
for(it = s.begin(); it != s.end(); it++){
if((*it).name=="zhao"){
s.erase(it);
}
}
//s.size()
cout<<s.size()<<endl;
return 0;
}
阅读全文
0 0
- set /?
- Set
- set
- set
- set
- Set
- set
- Set
- Set
- set
- set
- Set
- set
- Set
- set
- Set
- set
- Set
- 编码详解
- 自定义Activity模板
- 数据库-约束
- Mqtt精髓系列之安全
- shiro的注解
- Set
- 正则表达式扒取豆瓣定影排行
- 职责链模式(java实现)
- leetcode解题方案--059--Spiral Matrix II
- Echarts世界地图的国家名称中文化
- Indy10 IdudpServer获取字符串数据Read事件,TIdBytes类型的使用,支持中文
- 程序员一事无成,奈何桥上走一走
- 字符流中第一个不重复的字符(字符串)
- 一只小蜜蜂