set基本概念

1、简介:所有元素都会在插入时自动排序

2、本质:set/multiset属于关联式容器,底层结构是用二叉树实现

3、set和multiset区别:

①set不允许容器中有重复的元素

②multiset允许容器中有重复的元素

set构造和赋值

1、功能描述:创建set容器以及赋值

2、构造:

①set<T> st; //默认构造函数

②set(const set &st);//拷贝构造函数

3、赋值

①set& operator=(const set &st); //重载等号操作符

#include<set>
#include<string>

void printSet(set<int>&s){
    for(set<int>::iterator it=s.begin();it!=s.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
}

void test01(){
    set<int>s1;
    //插入数据,只有insert方式
    set.insert(10);
    set.insert(40);
    set.insert(30);
    set.insert(20);
    set.insert(30);
    //set容器特定:set不允许容器中有重复的元素,所有元素都会在插入时自动排序
    printSet(s1);

    set<int>s2(s1);
    printSet(s2);

    set<int>s3;
    s3=s2;
    printSet(s3);
}

set大小和交换

1、功能描述:统计set容器大小以及交换set容器

2、函数原型:

①size( ); //返回容器中元素的个数

②empty( ); //判断容器是否为空

③swap(st); //交换两个集合容器

#include<set>
#include<string>

void printSet(set<int>&s){
    for(set<int>::iterator it=s.begin();it!=s.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
}

void test01(){
    set<int>s1;
    //插入数据,只有insert方式
    set.insert(10);
    set.insert(30);
    set.insert(20);
    set.insert(40);
    printSet(s1);

    //判断是否为空
    if(s1.empty()){
        cout<<"s1为空"<<endl;
    }
    else{
        cout<<"s1不为空"<<endl;
        cout<<"s1的大小"<<s1.size()<<endl;
    }  
}

void test02(){
    set<int>s1;
    //插入数据,只有insert方式
    set.insert(10);
    set.insert(30);
    set.insert(20);
    set.insert(40);
    set<int>s2;
    //插入数据,只有insert方式
    set.insert(100);
    set.insert(300);
    set.insert(200);
    set.insert(400);   
    cout <<"交换前:"<<endl;
    printSet(s1);
    printSet(s2);

    cout <<"交换后:"<<endl;
    s1.swap(s2);
    printSet(s1);
    printSet(s2);
}

set插入和删除

1、功能描述:set容器进行插入数据和删除数据

2、函数原型:

①insert(elem); //在容器中插入数据

②clear( ); //清除所有元素

③erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器

④erase(beg,end); //删除区间[beg,end]的所有元素,返回下一个元素的迭代器

⑤erase(elem); //删除容器中值为elem的元素

#include<set>
#include<string>

void printSet(set<int>&s){
    for(set<int>::iterator it=s.begin();it!=s.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
}

void test01(){
    set<int>s1;
    //插入数据,只有insert方式
    set.insert(10);
    set.insert(30);
    set.insert(20);
    set.insert(40);
    printSet(s1);

    //删除 
    s1.erase(s1.begin());
    printSet(s1);

    //删除重载版本
    s1.erase(30);
    printSet(s1);

    //清空
    s1.erase(s1.begin(),s1,end());
    s1.clear();
    printSet(s1);
    
}

set查找和统计

1、功能描述:对set容器进行查找数据以及统计数据

2、函数原型:

①find(key); //查找key是否存在,若存在,返回该键元素的迭代器,若不存在,返回set.end( )

②count(key); //统计key的元素个数

#include<set>
#include<string>

void printSet(set<int>&s){
    for(set<int>::iterator it=s.begin();it!=s.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
}

void test01(){
    set<int>s1;
    //插入数据,只有insert方式
    set.insert(10);
    set.insert(30);
    set.insert(20);
    set.insert(40);

    set<int>::iterator pos=s1.find(30);
    if(pos!=a1.end()){
        cout<<"找到元素"<<*pos<<endl;
    }
    else{
        cout<<"未找到元素"<<endl;
    }
}

void test02(){
    set<int>s1;
    //插入数据,只有insert方式
    set.insert(10);
    set.insert(30);
    set.insert(20);
    set.insert(40);
    set.insert(30);
    set.insert(30);

    //对于set而言,统计的结果要不是0要不是1
    int num=s1.count(30);
    cout<<num<<endl;
}

set和multiset区别

1、学习目标:掌握set和multiset的区别

2、区别:

①set不可以插入重复数据,而multiset可以

②set插入数据的同时会返回插入结果,表示插入是否成功

③multiset不会检测数据,因此可以插入重复数据

#include<set>

void test01(){
    set<int> s;
    pair<set<int>::iterator,bool> ret=s.insert(10);
    if(ret.second){
        cout<<"第一次插入成功"<<endl;
    }
    else{
        cout<<"第一次插入失败"<<endl;
    } 
    //set不可以插入重复数据
    ret=s.insert(10);
     if(ret.second){
        cout<<"第二次插入成功"<<endl;
     }
     else{
        cout<<"第二次插入失败"<<endl;
     }  

     //multiset不会检测数据,可以插入重复数据
     multiset<int>ms;
     ms.insert(10);
     ms.insert(10);
     for(multiset<int>::iterator it=ms.begin();it!=ms.end();it++){
         cout<<*it<<" ";
     }
     cout<<endl;
}

pair对组创建

1、功能描述:成对出现的数据,利用对组可以返回俩个数据

2、两种创建方式:

①pair<type,type> p(value1,value2);

②pair<type,type> p=make_pair(value1,value2);

#include <string>

void test01(){
    pair<string,int> p(string("Tom"),20);//pair<string,int> p("Tom",20);
    cout<<"姓名"<<p.first<<"年龄"<<p.second<<endl;

    pair<string,int> p2=make_pair("Jerry",10);
    cout<<"姓名"<<p2.first<<"年龄"<<p2.second<<endl;
}

set容器排序

1、学习目标:set容器默认排序规则为从小到大,掌握如何改变排序规则

2、主要技术点:利用仿函数,可以改变排序规则

实例一:set存放内置数据类型

#include <set>

class MyCompare{
public:
    bool operator()(int v1,int v2){
        return v1>v2;
    }
}

void test01(){
    set<int> s1;
    s1.insert(10);
    s1.insert(40);
    s1.insert(20);
    s1.insert(50);
    s1.insert(30);
    for(set<int>::iterator it=s1.begin();it!=s1.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;

    //指定排序规则从大到小
    set<int,MyCompare> s2;
    s2.insert(10);
    s2.insert(40);
    s2.insert(20);
    s2.insert(50);
    s2.insert(30);  
    for(set<int,MyCompare>::iterator it=s2.begin();it!=s2.end();it++){
        cout<<*it<<" ";
    }  
    cout<<endl;
}

实例二:set存放自定义数据类型

#include <set>
#include<string>

class Person(){
public:
    Person(string name,int age){
        this->m_Name=name;
        this=>m_Age=age;
    }
    string m_Name;
    int m_Age;
}

class comparePerson{
public:
    bool operator()(const Person&p1,const Person&p2){
        return p1.m_Age>p2.m_Age;
    }
}

void test01(){
    //自定义数据类型都会指定排序规则
    set<Person,comparePerson> s;
    Person p1("刘备",24);
    Person p2("赵云",28);
    Person p3("张飞",25);
    Person p4("关羽",21);
    s1.insert(p1);
    s1.insert(p2);
    s1.insert(p3);
    s1.insert(p4);
    for(set<Person,comparePerson>::iterator it=s.begin();it!=s.end();it++){
        cout<<"姓名"<<it->m_Name<<"年龄"<<it->m_Age<<endl;
    }
}

Logo

码道开发者社区,聚焦华为云码道 CodeArts 代码智能体,沉淀 Agent、Skill、鸿蒙开发实战内容,供开发者查阅资料、交流技术、分享工程实践

更多推荐