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的更多相关文章

  1. STL map容器常用API

    map容器:键值和实值是分开的,排序规则按照键值排序 #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<map& ...

  2. 2.3 C++STL vector容器详解

    文章目录 2.3.1 引入 2.3.2 代码实例 2.3.3 运行结果 总结 2.3.1 引入 vector 容器 动态数组 可变数组 vector容器 单口容器(尾部操作效率高) vector动态增 ...

  3. 2.4 C++STL deque容器详解

    文章目录 2.4.1 引入 2.4.2 代码示例 2.4.3 代码运行结果 2.4.4 具体案例 总结 2.4.1 引入 deque容器类比vector容器来学习. deque为双向开口容器,见下图. ...

  4. STL vector常用API

    1.容器:序列容器(时间决定).关联式容器(容器中的数据有一定规则) 2.迭代器:通过迭代器寻找.遍历容器中的数据 vetor的使用:数据遍历与输出 #define _CRT_SECURE_NO_WA ...

  5. C++ STL中的常用容器浅谈

    STL是C/C++开发中一个非常重要的模板,而其中定义的各种容器也是非常方便我们大家使用.下面,我们就浅谈某些常用的容器.这里我们不涉及容器的基本操作之类,只是要讨论一下各个容器其各自的特点.STL中 ...

  6. 深入解析C++ STL中的常用容器

    转载:http://blog.csdn.net/u013443618/article/details/49964299 这里我们不涉及容器的基本操作之类,只是要讨论一下各个容器其各自的特点.STL中的 ...

  7. Set容器——HashSet及常用API

    Set容器特点: ①   Set容器是一个不包含重复元素的Collection,并且最多包含一个null元素,它和List容器相反,Set容器不能保证其元素的顺序; ②   最常用的两个Set接口的实 ...

  8. Map容器——TreeMap及常用API,Comparator和Comparable接口

    TreeMap及常用API ①   TreeMap类通过使用红黑树实现Map接口; ②   TreeMap提供按排序顺序存储键/值对的有效手段,同时允许快速检索; ③   不像散列(HashMap), ...

  9. List容器——LinkedList及常用API,实现栈和队列

    LinkedList及常用API ①   LinkedList----链表 ②   LinkedList类扩展AbstractSequentialList并实现List接口 ③   LinkedLis ...

随机推荐

  1. SpringBoot课程学习(二)

    一.断言 (1).@assertTrue,@assertFalse assertTrue与assertFalse用来判断条件是否为true或false,assertTrue表示如果值为true则通过, ...

  2. vue中a标签地址传参

     注意:  1)href前面加冒号"  :  ".     2)字符串用单引号包裹 .     3)传过去数值用+号连接 传值:<li class="list-li ...

  3. day07-2MySQL索引

    MySQL索引 说起提高数据库性能,索引是最物美价廉的东西了.不用加内存,不用改程序,不用调sql,查询速度就能提高千百倍. 例子 首先,创建一个有800万条数据的表 -- 创建测试数据库 tmp C ...

  4. SpringMvc(五) - 支付宝沙箱和关键字过滤,md5加密,SSM项目重要知识点

    1.支付宝沙箱 1.1 jar包 alipay-sdk <!-- alipay-sdk --> <dependency> <groupId>com.alipay.s ...

  5. 可观测性的常见用例|Techtarget

    [ 文章来源 ]https://www.techtarget.com/searchitoperations/tip/Common-use-cases-for-observability 这些可观测性用 ...

  6. 洛谷P6060 [加油武汉]传染病研究

    一道不错的数学题 Solution 看到约数个数就想到枚举约数,但对于每个询问都枚举显然不现实,但是我们可以将大致的方向锁定在这方面,是否可以预处理出一定的东西,然后低复杂度询问呢? 我们想到预处理出 ...

  7. 论文解读(GGD)《Rethinking and Scaling Up Graph Contrastive Learning: An Extremely Efficient Approach with Group Discrimination》

    论文信息 论文标题:Rethinking and Scaling Up Graph Contrastive Learning: An Extremely Efficient Approach with ...

  8. PX01关于手机屏IIC触摸调试学习笔记

    相关文件下载: 上位机工具:http://www.xk-image.com/download/blog/0002_TP调试/LcdTools20210605.rar 调试案例:http://www.x ...

  9. Cookie、Session、Token与JWT(跨域认证)

    之前看到群里有人问JWT相关的内容,只记得是token的一种,去补习了一下,和很久之前发的认证方式总结的笔记放在一起发出来吧. Cookie.Session.Token与JWT(跨域认证) 什么是Co ...

  10. 【Bluetooth|蓝牙开发】二、蓝牙开发入门

    个人主页:董哥聊技术 我是董哥,嵌入式领域新星创作者 创作理念:专注分享高质量嵌入式文章,让大家读有所得! [所有文章汇总] 1.蓝牙基础概念 蓝牙,是一种利用低功率无线电,支持设备短距离通信的无线电 ...