【set容器】

shinnyblue / 2023-06-25 / 原文

set

定义

set又名集合,是一种内部自动排序、且不含重复元素的容器
● 有序
● 去重

遍历

只能用迭代器遍历

#include <set>
#include <cstdio>
using namespace std;
int main(){
    set<int> st;
    for (int i = 6; i > 0; i--)
    {
        st.insert(i);
    }
    for (set<int>::iterator it=st.begin();it!=st.end(); it++)
    {
        printf("%d ",*it); // 输出:1 2 3 4 5 6
    }

cout<<endl;

    for (auto x : st)
    {
        cout<<x<<' '; // 输出:1 2 3 4 5 6
    }
    return 0;
}

insert()

向容器内插入元素

st.insert(value); // 插入的元素被自动排序后放在对应的位置

find()

find()函数的返回值为迭代器类型。

// 核心代码
set<int>::iterator it = st.find(2);
#include<iostream>
#include<set>
using namespace std;
set<int> s;//定义set
int n;
int main()
{
    cout<<"请输入n:"<<'\n';
    cin>>n;

    cout<<"请输入n个元素:"<<'\n';
    for(int i=0;i<n;i++)
    {
        int t;
        cin>>t;
        s.insert(t);//插入数据到set中
    }

    cout<<"容器内元素如下:"<<'\n';
    for (auto x : s)
    {
        cout<<x<<' ';
    }
    cout<<'\n';
    cout<<"请输入要查找的数:"<<'\n';
    int ele;
    cin>>ele;
    if(s.find(ele)!=s.end()) // 如果集合容器 `st` 中不存在某个值 `value`,则 `st.find(value)` 函数会返回 `st.end()`.
    {
        cout<<ele<<" exists"<<endl;
    }else{
        cout<<"Not Found"<<endl;
    }
    return 0;
}
// 运行
请输入n:
5
请输入n个元素:
13 27 2 8 999
容器内元素如下:
2 8 13 27 999
请输入要查找的数:
68
Not Found

erase()

说到删除,集合容器的删除分删除单个元素删除一段区间两种



  • 删除单个元素

①删除迭代器

st.erase(it)
#include<iostream>
#include<set>
using namespace std;
set<int> s;//定义set
int main()
{
    for (int i = 1; i <= 10; i++)
    {
        s.insert(i);
    }
    s.erase(s.find(4)); // 删除指定元素的迭代器
    for (set<int>::iterator it = s.begin(); it != s.end(); it++)
    {
        cout<<*it<<' ';
    }
    
    return 0;
}
// 输出:
// 1 2 3 5 6 7 8 9 10


②删除元素

#include<iostream>
#include<set>
using namespace std;
set<int> s;//定义set
int main()
{
    for (int i = 1; i <= 10; i++)
    {
        s.insert(i);
    }
    s.erase(4); // 删除指定元素
    for (set<int>::iterator it = s.begin(); it != s.end(); it++)
    {
        cout<<*it<<' ';
    }
    
    return 0;
}
// 输出:
// 1 2 3 5 6 7 8 9 10



  • 删除区间

删除一个区间内的所有元素

#include <set>
#include <iostream>
using namespace std;
int main()
{
    set<int> st;
    for (int i = 1; i <= 10; i++)
    {
        st.insert(i);
    }
    set<int>::iterator l = st.find(2);
    set<int>::iterator r = st.find(5);
    st.erase(l, r); // 删除区间:[2,5)
    for (set<int>::iterator it = st.begin(); it != st.end(); it++)
    {
        cout<<*it<<' '; // 输出: 1 5 6 7 8 9 10 
    }
    return 0;
}


size()

获得set容器内元素的个数

#include <set>
#include <cstdio>
using namespace std;
int main(){
    set<int> st;
    for (int i = 100; i <=500; )
    {
        st.insert(i);//100 200 300 400 500
        i+=100;
    }
    printf("%d",st.size());//输出:5
    
    return 0;
}

clear()

清空容器内所有的元素

#include <set>
#include <cstdio>
using namespace std;
int main(){
    set<int> st;
    for (int i = 100; i <=500; )
    {
        st.insert(i);//100 200 300 400 500
        i+=100;
    }
    st.clear();//清空
    st.insert(999);
    set<int>::iterator it = st.begin();
    printf("%d",*it);//输出:999
    
    return 0;
}

set的用途

①去重

②升序排序