STL:STL关联容器 set

阅读: 评论:0

STL:STL关联容器 set

STL:STL关联容器 set

文章目录

  • STL关联容器 set
  • 1. 操作
    • 1.1 使用迭代器遍历
    • 1.2 插入
    • 1.3 查找
  • 2. 相关练习
    • 2.1 力扣217:存在重复元素
    • 2.2 力扣448:找到所有数组中消失的数字
    • 2.3 力扣287:寻找重复数
    • 2.4 力扣349:两个数组的交集
  • 3. 容器易解应用题
    • 3.1 51nod1912:咖啡馆
    • 3.2 51nod1915:西湖游船

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);          // 插入数字10if(res.second){              // 插入成功即插入不重复,res.second为truecout << *(res.first) << "插入成功" << endl;    // *(res.first)是具体数值}else{                       // 插入失败即插入数值重复cout << "插入失败" << endl

本文发布于:2024-01-31 19:19:46,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170669998830770.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:容器   STL   set
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23