集合容器(set container in STL)

组别:提高级
难度:5

集合是一个元素有序且不重合的容器。一般使用平衡树作为底层数据结构。

集合(set)的基本操作

成员函数(member function)overview
st.empty() 判断是否为空
st.size()返回集合中元素个数
st.clear()擦除所有元素
st.insert(value)在集合中插入元素
st.erase(value)从集合中删除给定值的元素
st.erase(postion)从集合中删除位于position位置的元素
st.erase(first, last)从集合中删除迭代器范围为[first, last-1]中的元素
st.find(value)从集合中给定值对应的迭代器,如果不存在则返回end()
st.count(value)查询一个给定值在集合中的个数,因为是不可重集,所以个数不超过1
st.lower_bound(value)查询集合中不小于给定值的迭代器,如果不存在则返回end()
st.upper_bound(value)查询集合中大于给定值的迭代器,如果不存在则人返回end()
st.swap(st1)交换两个集合的值
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>  
#include <set>  
  
int main() {  
    std::set<int> mySet;  
  
    // 插入元素  
    mySet.insert(3);  
    mySet.insert(1);  
    mySet.insert(4);  
    mySet.insert(1); // 不会插入重复的元素  
  
    // 遍历并打印元素  
    for (int num : mySet) {  
        std::cout << num << " ";  
    }  
    std::cout << std::endl; // 输出: 1 3 4  
  
    // 查找元素  
    if (mySet.find(3) != mySet.end()) {  
        std::cout << "Found 3 in the set." << std::endl;  
    }  
  
    // 删除元素  
    mySet.erase(4);  
  
    // 再次遍历并打印元素  
    for (int num : mySet) {  
        std::cout << num << " ";  
    }  
    std::cout << std::endl; // 输出: 1 3  
  
    return 0;  
}

多重集合(multiset)

multiset是维护有序可重集合的容器,基本操作与set类似,以下是其特殊方法。

多重集合特有方法

成员函数功能
ms.erase(value)从集合中删除给定值的所有元素
ms.equal_range(value)查询一个给定值在集合中出现的范围,用pair的形式返回迭代器对

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <set>
using namespace std;

int main(){
    set<int> st0;  //创建一个集合
    set<int> st1= {6, 10, 5, 1}; // 创建一个有初始值的集合
    set<int,greater<int> > st2; //创建一个集合,元素降序
    multiset<int> st3;  //创建一个多重集合
    
    st0.insert(4);
    st0.insert(3);
    st0.insert(6);
    st0.insert(9);
    st0.insert(5);
    st0.insert(6);
    
    st2.insert(5);
    st2.insert(7);
    st2.insert(3);
    
    st3.insert(5);
    st3.insert(7);
    st3.insert(5);
    
    cout<<st0.size()<<'\n';
    for( auto i:st0)cout<<i<<' ';
    cout<<'\n';
    
    st0.swap(st1);  //交投st0和st1中的元素
    for( auto i:st0)cout<<i<<' ';
    cout<<'\n';
    
    for(auto i:st1)cout<<i<<' ';    
    cout<<'\n';
    
    for(auto i:st2)cout<<i<<' ';    
    cout<<'\n';
    
    for(auto i:st3)cout<<i<<' ';    
    
    
}
Scroll to Top