集合容器(set):快速查找,不允许重复值
set 的特点:有序且不重复
#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
将某一个数值插入到数组中
#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小时内删除。
留言与评论(共有 0 条评论) |