C++ STL set容器常用用法

set是STL中一种标准关联容器。它底层使用平衡的搜索树——红黑树实现,插入删除操作时仅仅需要指针操作节点即可完成,不涉及到内存移动和拷贝,所以效率比较高。set,顾名思义是“集合”的意思,在set中元素都是唯一的,而且默认情况下会对元素自动进行升序排列,支持集合的交(set_intersection),差(set_difference) 并(set_union),对称差(set_symmetric_difference) 等一些集合上的操作,如果需要集合中的元素允许重复那么可以使用multiset。

1.set容器的常用操作

使用时注意包含头文件<set>    std::set and std::multiset associative containers

s.begin()      返回set容器的第一个元素

s.end()      返回set容器的最后一个元素

s.clear()       删除set容器中的所有的元素

s.empty()     判断set容器是否为空

s.insert()      插入一个元素

s.erase()       删除一个元素

s.size()     返回当前set容器中的元素个数

set模板原型://Key为元素(键值)类型

template <class Key, class Compare=less<Key>, class Alloc=STL_DEFAULT_ALLOCATOR(Key) >

2.set容器的创建

#include <iostream>
#include <set>
#include <functional>
using namespace std;
set<int> s;

int main(){
   set<int > seta; //默认是小于比较器less<int>的set

   set<int, greater<int> > setb; //创建一个带大于比较器的set,需包含头文件functional

   int a[5] = {1,2,3,4,5};
   set<int > setc(a,a+5); //数组a初始化一个set;

   set<int > setd(setc.begin(),setc.end()); //setc初始化一个set
   //上述两例均为区间初始化

   set<int > sete(setd); //拷贝构造创建set
   return 0;
}

3.set容器的增删改查

①插入

#include <iostream>
#include <set>
using namespace std;
set<int >s;
void setprint(int cnt){
    cout << "Test output :" << cnt << ":" << endl;
    for(set<int>::iterator it = s.begin(); it!= s.end(); it++)
        cout << *it << " ";
    puts("");
    return ;
}
int main(){
    int cnt = 1;
    s.insert(1);
    s.insert(2);
    s.insert(5);
    setprint(cnt++);

    s.insert(2); //set只允许用一个值出现一次,要插入相同元素请用multiset
    setprint(cnt++);

    int a[4] = {11,12,13,14};
    s.insert(a,a+4); //将区间[a, a+4]里的元素插入容器
    setprint(cnt++);

    return 0;
}

插入

②删除

s.erase()       删除一个元素

s.clear()       删除set容器中的所有的元素
#include <iostream>
#include <set>
using namespace std;
set<int >s;
void setprint(int cnt){
    cout << "Test output :" << cnt << ":" << endl;
    for(set<int>::iterator it = s.begin(); it!= s.end(); it++)
        cout << *it << " ";
    puts("");
    return ;
}

int main(){
    int cnt = 1;
    for(int i = 1; i < 11; i++){
        s.insert(i);
    }
    setprint(cnt++);

    s.erase(9); //根据元素删除
    setprint(cnt++);

    set<int>::iterator ita = s.begin();
    set<int>::iterator itb = s.begin();
    s.erase(ita);  //删除迭代器指向位置的元素
    setprint(cnt++);

    ita = s.begin();
    itb = s.begin();
    itb++;itb++;
    s.erase(ita,itb); //删除区间[ita,itb)的元素
    setprint(cnt);
    s.clear();
    return 0;
}

删除

③修改

不能直接修改容器内数据,所以只能删除某元素再插入要修改的数值。

④查找

s.find()        查找一个元素,如果容器中不存在该元素,返回值等于s.end()
#include <iostream>
#include <set>
using namespace std;
set<int >s;
void setprint(int cnt){
    cout << "Test output :" << cnt << ":" << endl;
    for(set<int>::iterator it = s.begin(); it!= s.end(); it++)
        cout << *it << " ";
    puts("");
    return ;
}
int main(){
    int cnt = 1;
    s.insert(1);
    s.insert(2);
    s.insert(5);
    setprint(cnt++);

    if(s.find(2) != s.end()) 
        cout << "2 is existent" << endl;
    else 
        cout << "2 is non-existent" << endl;
    if(s.find(3) == s.end()) 
        cout << "3 is non-existent" << endl;
    else 
        cout << "2 is existent" << endl;
    return 0;
}

查找


4.set的其他常用操作

s.lower_bound() 返回第一个大于或等于给定关键值的元素

s.upper_bound() 返回第一个大于给定关键值的元素

s.equal_range() 返回一对定位器,分别表示 第一个大于或等于给定关键值的元素 和 第一个大于给定关键值
                的元素,这个返回值是一个pair类型,如果这一对定位器中哪个返回失败,就会等于
                s.end()
#include <iostream>
#include <set>
using namespace std;

int main(){
    set<int> s;
    s.insert(1);
    s.insert(2);
    s.insert(5);

    cout << "lower_bound & upper_bound test:" << endl;

    cout << "第一个大于或等于3的元素: " << *s.lower_bound(3) << endl;
    cout << "第一个大于或等于2的元素: " <<*s.lower_bound(2) << endl;
    cout << "第一个大于2的元素: " <<*s.upper_bound(2) << endl;

    cout << "equal_range test:" << endl;

    cout << "第一个大于或等于2的元素: " <<  *s.equal_range(2).first << endl;
    cout << "第一个大于2的元素: " << *s.equal_range(2).second << endl;
    return 0;
}

样例

②判断元素是否在set中 & 判断set是否为空

#include <iostream>
#include <set>
#include <functional>
using namespace std;

int main(){
    set<int > s;
    if(s.empty()) cout << "容器为空" << endl;
    s.insert(1);
    if(!s.empty()) cout << "容器不为空" << endl;

    if(s.count(1)) cout << "1在容器中" << endl;
    if(!s.count(2)) cout << "2不在容器中" << endl;
    return 0;
}

是否为空

③自定义比较函数

#include <iostream>
#include <set>
#include <functional>
using namespace std;

struct cmp{
    bool operator () (const int &a, const int &b){
        return a > b;
    }
};
set<int, cmp>s; //自定义排序函数构造set
void setprint(int cnt){
    cout << "Test output :" << cnt << ":" << endl;
    for(set<int,cmp>::iterator it = s.begin(); it!= s.end(); it++)
        cout << *it << " ";
    puts("");
    return ;
}
int main(){
    s.insert(1);
    s.insert(2);
    s.insert(6);
    setprint(1);
    return 0;
}

自定义比较函数


以上就是刷题必备的set用法,熟练掌握,要用时别用错就成。

至于求并、交、差、对称差等操作,暂不细说,使用时要包含头文件”algorithm”。
此外还有unordered_set和unordered_multiset,为set和multiset的无序版,使用时要包含头文件”unordered_set”。

  • 49
    点赞
  • 179
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
本版本与网上其他资源不同之处在于,此版本可进行编辑,搜索,已进行内容识别扫描。可全选,可编辑,可剪切文字。 部分目录如下: 目录 第一篇预备知识 第1 章C++ 编程技术...................................................... 3 1-1 C++与C 语言的区别................................................... 4 1-1-1 文件扩展名的改变,.............................................. 4 1-1-2 简化输入/输出手段.............................................. 5 1-1-3 数据类型声明的改变,............................................ 5 1-1-4 动态内存分配运算符的使用....................................... 6 1-1-5 弓I 用(References) 类型, ··················•················•"'''8 1-1-6 const 语义的扩展................................................ 9 1-1-7 指针声明类型与对象类型相一致.................................. 13 1-1-8 int 与char 不再等价............................................. 13 1-1-9 结构数据类型的变化............................................ 13 1-1-10 数组和指针技术的不同......................................... 14 1-2 C++存储技术........................................................ 15 1-2 一I C++存储类型.................................................. 15 I6I7 ..... .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. •. .• •. .• .. .. .. .. .. .. .. .. .. .. .. .. . 期 符存 饰生 修的 取象 存对 廿廿 I2I32 ~3 c c 1-3 C++ 函数技术........................................................ 19 1-3-1 类的构造函数、析构函数与赋值函数,..... - ........ - .............. 19 1-3-2 在派生类中实现类的基本函数,................... _ ............... 29 1-3-3 内联函数技术,........ ................................... 30 3133 ..... .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. . 现 实 术的 技制 数机 函象 元对 友向 由 曰1. l -C 1 4 3337 ..... .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. . 术术 技技 承载 继重 的数 类函 l4l44 3 ~ 3840 ..... .. .. .. .. .. .. •. .• .. .. .. .. .. .. .. .. .. .. .. •. .• .. •. .• •. •• .• .. .. .. .. .. .. .. .. .. •. •• .• . 术 技 类 术象 技抽 载和 重数 符函 算虚 运纯 l4l34 4 1-5 小结...............................................

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值