STL:STL关联容器 set

STL关联容器 set

集合容器(set):快速查找,不允许重复值

set 的特点:有序且不重复

1. 操作

1.1 使用迭代器遍历

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

int main(){
   
        // 迭代器遍历
        set<int> s = {
   1,5,3,7,3};
        for(auto it = s.begin();it != s.end();++it){
   
                cout << *it << " ";
        }
        cout << endl;

        for(auto n:s){
   
                cout << n << " ";
        }
        cout << endl;
}

结果为:

1 3 5 7 
1 3 5 7

1.2 插入

将某一个数值插入到数组中

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

int main(){
   
        set<int> s = {
   1,5,3,7,3};

        for(auto n:s){
   
                cout << n << " ";
        }
        cout << endl;

        // 添加数据
        // pair<set<int>::iterator,bool> res = s.insert(10);
        auto res = s.insert(10);          // 插入数字10
        if(res.second){
                 // 插入成功即插入不重复,res.second为true
                cout << *(res.first) << "插入成功" << endl;    // *(res.first)是具体数值
        }else{
                          // 插入失败即插入数值重复
                cout << "插入失败" << endl
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
STL中使用哈希表的容器有unordered系列关联容器。这些容器采用了哈希的思想,存储和查找数据的效率非常高,能够达到理想的O(1)。\[2\] unordered系列关联容器包括unordered_set、unordered_multiset、unordered_map和unordered_multimap。这些容器使用哈希函数将元素映射到桶中,以实现快速的插入、查找和删除操作。\[2\] 此外,还可以使用布隆过滤器来支持删除操作,通过将布隆过滤器中的每个比特位扩展成一个计数器,对应的哈希函数计算出的哈希值对应的计数器进行加减操作,以实现删除操作。\[3\] #### 引用[.reference_title] - *1* [【数据结构】—— 哈希表/STL使用技巧](https://blog.csdn.net/forever_bryant/article/details/126068847)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [<stl系列>哈希表详解,掌握stl容器从现在开始](https://blog.csdn.net/weixin_59371851/article/details/126917526)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值