STL之set(集合)(3)

set类似于vector,但是set(集合)中元素不可重复。

  • set的定义、初始化、基本操作:
 {
    set <string> m;
    m.insert("hello1");
    m.insert("hello2");
    m.insert("hello3");
    if (find(m.begin(),m.end(), "hello") == m.end())//没有找到
     cout << "no" << endl;
    else 
     cout<<"yes"<<endl;
}
  • set基本操作函数
#include<bits/stdc++.h> 
using namespace std;
/*
    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() 判断元素k是否在容器内  输出1 
    cout<<s.count(a)<<endl;
    //若a存在,则删除元素a   输出1 2 6
    s.erase(a);  
    //s.find() 寻找k,若找到返回对应的迭代器,否则返回end();
    cout<<*s.find(2)<<":寻找到k,输出k"<<endl;
    //s.size()返回容器现有元素个数 输出3
    cout<<s.size()<<endl;
    //s.empty()原来的数据为非空,假,输出0 
    cout<<s.empty()<<endl;
    //定义正向迭代器
    set <int>::iterator it;
    for(it=s.begin();it!=s.end();it++)
    {
      cout<<*it<<" ";
    }
    cout<<endl;
    // 定义反向迭代器 
//----------
    for(rit=s.rbegin();rit!=s.rend();rit++)
    {
      cout<<*rit<<" ";
    }
}
  • 结构体+set

    题目:输入3名学生的名字和成绩,最后把名为“张三”的删除。

#include<bits/stdc++.h>
using namespace std;
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=="张三")
        {
         s.erase(it);
        }
    } 
    //s.size()  
    cout<<s.size()<<endl; 
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhaoshuling1109/article/details/80489225
今日推荐