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. 分布式存储系统之Ceph集群存储池操作

    前文我们了解了ceph的存储池.PG.CRUSH.客户端IO的简要工作过程.Ceph客户端计算PG_ID的步骤的相关话题,回顾请参考https://www.cnblogs.com/qiuhom-187 ...

  2. Spring的同一个服务为什么会加载多次?

    问题现象 最近在本地调试公司的一个Web项目时,无意中发现日志中出现了两次同一个服务的init记录,项目都是基于Spring来搭建的,按理说服务都是单例的,应该只有一次服务加载日志才对,本着对工作认真 ...

  3. C语言常见的八大排序(详解)

    冒泡排序 优点:写起来简单 缺点:运算量过大每两个之间就要比较一次 冒泡排序在一组需要排序的数组中,对两两数据顺序与要求顺序相反时,交换数据,使大的数据往后移,每趟排序将最大的数放在最后的位置上 如下 ...

  4. UDP协议的网络编程

    public class UDPTest { //发送端@Testpublic void sender() throws IOException { DatagramSocket socket = n ...

  5. for循环小九九乘法表

    for(int i=1;i<=9;i++) { for(int j=1;j<=i;j++) { System.out.print(i+"*"+j+"=&quo ...

  6. 2.MongoDB系列之创建更新删除文档

    1. 插入文档 // 单条插入 db.getCollection('blog').insertOne({'type': 'mongodb'}) // 批量 插入 db.getCollection('b ...

  7. Audacity开源音频处理软件使用入门

    操作系统 :Windows10_x64 Audacity版本:3.2.1 Audacity是一款开源.免费.跨平台的音频处理及录音软件,支持Windows.macOS及Linux操作系统. 这里记录下 ...

  8. [Go疑难杂症]为什么nil不等于nil

    现象 在日常开发中,可能一不小心就会掉进 Go 语言的某些陷阱里,而本文要介绍的 nil ≠ nil 问题,便是其中一个,初看起来会让人觉得很诡异,摸不着头脑. 先来看个例子: type Custom ...

  9. vue2和vue3组合使用教程地址

    https://cn.vuejs.org/guide/essentials/watchers.html#eager-watchers

  10. 云原生之旅 - 5)Kubernetes时代的包管理工具 Helm

    前言 上一篇文章 [基础设施即代码 使用 Terraform 创建 Kubernetes] 教会了你如何在Cloud上面建Kubernetes资源,那么本篇来讲一下如何在Kubernetes上面部署应 ...