STL set容器常用API
set容器,容器内部将数据自动排序(平衡二叉树),不能插入重复元素。multiset可以插入重复元素。不能修改容器中的值,通过删除值,在插入。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<set>
#include<string>
#include<algorithm>
using namespace std; struct MyComapre{
bool operator()(int v1, int v2){
return v1 > v2;
}
}; void printMySet(set<int, MyComapre> &s){ for (set<int, MyComapre>::iterator it = s.begin(); it != s.end(); ++it){
cout << *it << " ";
}
cout << endl;
} //1.
/*
set<T> st;//set默认构造函数:
mulitset<T> mst; //multiset默认构造函数:
set(const set &st);//拷贝构造函数 insert(elem);//在容器中插入元素。
clear();//清除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg, end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(elem);//删除容器中值为elem的元素。 */ void test01(){ set<int, MyComapre> s;
s.insert(4);
pair<set<int, MyComapre>::iterator, bool> ret = s.insert(2);
if (ret.second){
cout << "第一次插入成功!" << endl;
}
else{
cout << "第一次插入失败!" << endl;
}
s.insert(3);
s.insert(9);
s.insert(6); ret = s.insert(2);
if (ret.second){
cout << "第一次插入成功!" << endl;
}
else{
cout << "第一次插入失败!" << endl;
} //删除第一个元素的
s.erase(2);
s.erase(s.begin());
//set容器迭代器什么类型?双向迭代器 printMySet(s);
} void print(const int &val){
cout << val << " ";
} void test02(){ multiset<int> ms;
ms.insert(7);
ms.insert(4);
ms.insert(9);
ms.insert(2);
ms.insert(10);
ms.insert(2); for_each(ms.begin(), ms.end(), print);
cout << endl;
} //3. set查找操作
/*
find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key);//查找键key的元素个数
lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
*/ void test03(){ set<int> s;
s.insert(1);
s.insert(2);
//s.insert(3);
s.insert(4);
s.insert(5); set<int>::iterator it = s.find(12);
if (it == s.end()){
cout << "查找失败!" << endl;
}
else{
cout << "查找到的值是:" << *it << endl;
} cout << "值为2的元素有:" << s.count(12) << "个!" << endl; //lower_bound upper_bound
it = s.lower_bound(2);
if (it == s.end()){
cout << "查找失败!" << endl;
}
else{
cout << "查找到的值是:" << *it << endl;
} it = s.upper_bound(2);
if (it == s.end()){
cout << "查找失败!" << endl;
}
else{
cout << "查找到的值是:" << *it << endl;
}
cout << "------------------------" << endl; pair<set<int>::iterator, set<int>::iterator> ret = s.equal_range(2);
cout << *(ret.first) << endl;
cout << *(ret.second) << endl; } //自定义数据类型
class Person{
public:
Person(string name,int age){
this->mName = name;
this->mAge = age;
}
public:
string mName;
int mAge;
}; struct PersonRule{
bool operator()(const Person &p1,const Person &p2){
return p1.mAge > p2.mAge;
}
}; void test04(){ set<Person, PersonRule> s; s.insert(Person("aaa", 10));
s.insert(Person("bbb", 70));
s.insert(Person("ccc", 50));
s.insert(Person("ddd", 30));
s.insert(Person("eee", 40)); for (set<Person, PersonRule>::iterator it = s.begin(); it != s.end();++it){
cout << "Name:" << it->mName << " Age:" << it->mAge << endl;
} cout << "----------------------" << endl; //!PersonRule()(p1, p2) && !PersonRule()(p2, p1) set<Person, PersonRule>::iterator it = s.find(Person("aaa", 20));
if (it == s.end()){
cout << "查找失败!" << endl;
}
else{
cout << "Name:" << it->mName << " Age:" << it->mAge << endl;
} } int main(){ //test01();
//test02();
//test03();
test04(); system("pause");
return EXIT_SUCCESS;
}
STL set容器常用API的更多相关文章
- STL map容器常用API
map容器:键值和实值是分开的,排序规则按照键值排序 #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<map& ...
- 2.3 C++STL vector容器详解
文章目录 2.3.1 引入 2.3.2 代码实例 2.3.3 运行结果 总结 2.3.1 引入 vector 容器 动态数组 可变数组 vector容器 单口容器(尾部操作效率高) vector动态增 ...
- 2.4 C++STL deque容器详解
文章目录 2.4.1 引入 2.4.2 代码示例 2.4.3 代码运行结果 2.4.4 具体案例 总结 2.4.1 引入 deque容器类比vector容器来学习. deque为双向开口容器,见下图. ...
- STL vector常用API
1.容器:序列容器(时间决定).关联式容器(容器中的数据有一定规则) 2.迭代器:通过迭代器寻找.遍历容器中的数据 vetor的使用:数据遍历与输出 #define _CRT_SECURE_NO_WA ...
- C++ STL中的常用容器浅谈
STL是C/C++开发中一个非常重要的模板,而其中定义的各种容器也是非常方便我们大家使用.下面,我们就浅谈某些常用的容器.这里我们不涉及容器的基本操作之类,只是要讨论一下各个容器其各自的特点.STL中 ...
- 深入解析C++ STL中的常用容器
转载:http://blog.csdn.net/u013443618/article/details/49964299 这里我们不涉及容器的基本操作之类,只是要讨论一下各个容器其各自的特点.STL中的 ...
- Set容器——HashSet及常用API
Set容器特点: ① Set容器是一个不包含重复元素的Collection,并且最多包含一个null元素,它和List容器相反,Set容器不能保证其元素的顺序; ② 最常用的两个Set接口的实 ...
- Map容器——TreeMap及常用API,Comparator和Comparable接口
TreeMap及常用API ① TreeMap类通过使用红黑树实现Map接口; ② TreeMap提供按排序顺序存储键/值对的有效手段,同时允许快速检索; ③ 不像散列(HashMap), ...
- List容器——LinkedList及常用API,实现栈和队列
LinkedList及常用API ① LinkedList----链表 ② LinkedList类扩展AbstractSequentialList并实现List接口 ③ LinkedLis ...
随机推荐
- P5658 [CSP-S2019] 括号树
对于特殊性质fi=i-1,原图是一条链,注意到当前节点是' ('不会产生贡献,')'才会产生,那么思考怎么的计算这个贡献. ()()():每个位置贡献是0,1,0,2,0,3.答案统计出来就是说0,1 ...
- 洛谷P1638 逛画展 (尺取法)
尺取法的经典题目: 博览馆正在展出由世上最佳的 mm 位画家所画的图画. 游客在购买门票时必须说明两个数字,aa 和 bb,代表他要看展览中的第 aa 幅至第 bb 幅画(包含 a,ba,b)之间的所 ...
- 常见的 Kerberos 错误消息
常见的 Kerberos 错误消息 问题:All authentication systems disabled; connection refused 原因:此版本的 rlogind 不支持任何验证 ...
- (数据科学学习手札144)使用管道操作符高效书写Python代码
本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,一些比较熟悉pandas的读者 ...
- 强国杯东杯分区赛miscwp
目录 不要被迷惑 PCAP文件分析 平正开 不要被迷惑 编辑 导出http 编辑 得到flag.zip后直接爆破密码 编辑 得到编辑 然后一键解码 编辑 flag{WImuJeqSNPh ...
- 《Java并发编程的艺术》读书笔记:二、Java并发机制的底层实现原理
二.Java并发机制底层实现原理 这里是我的<Java并发编程的艺术>读书笔记的第二篇,对前文有兴趣的朋友可以去这里看第一篇:一.并发编程的目的与挑战 有兴趣讨论的朋友可以给我留言! 1. ...
- golang中的字符串
0.1.索引 https://waterflow.link/articles/1666449874974 1.字符串编码 在go中rune是一个unicode编码点. 我们都知道UTF-8将字符编码为 ...
- NLP之基于BERT的预测掩码标记和句间关系判断
BERT @ 目录 BERT 程序步骤 程序步骤 设置基本变量值,数据预处理 构建输入样本 在样本集中随机选取a和b两个句子 把ab两个句子合并为1个模型输入句,在句首加入分类符CLS,在ab中间和句 ...
- python不确定性计算之粗糙集属性约简
粗糙集属性约简 本实验同时采用区别矩阵和依赖度约简. 在依赖度约简中,设置依赖度计算函数和相对约简函数,对读取的数据进行处理,最后根据依赖度约简. 在读取数据后判断有无矛盾,若有则进行决策表分解,然后 ...
- ubuntu下Mysql安装与root密码重置
一.安装 1.首先更新本地存储库索引,执行sudo apt update 2.从APT存储库安装MySQL,执行sudo apt install MySQL-server,在安装过程中,可能会出现[Y ...