set/multiset 的特性是所有元素会根据元素的值自动进行排序。set 是以 RB-tree(红黑树,平衡二叉树的一种)为底层机制,其查找效率非常好。set 容器中不允许重复元
素,multiset 允许重复元素。
我们可以通过 set 的迭代器改变元素的值吗?
答: 不行,因为 set 集合是根据元素值进行排序,关系到 set 的排序规则,如果任意改变 set 的元素值,会严重破坏 set 组织。
#include <iostream>
#include <set>
#include <list>
#include <string>
using namespace std; void PrintSet(set<int>& s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
} class mycompare
{
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
}; // set初始化
// set<T> st;//set 默认构造函数:
// mulitset<T> mst; //multiset 默认构造函数:
// set(const set &st);//拷贝构造函数
void test01()
{
set<int> s1; // 自动进行排序, 默认从小到大
s1.insert();
s1.insert();
s1.insert();
s1.insert();
s1.insert();
PrintSet(s1);
// 赋值操作
// set& operator=(const set &st);//重载等号操作符
// swap(st);//交换两个集合容器
set<int> s2;
s2 = s1;
PrintSet(s2);
// 删除操作
// insert(elem);//在容器中插入元素。
// clear();//清除所有元素
// erase(pos);//删除 pos 迭代器所指的元素,返回下一个元素的迭代器。
// erase(beg, end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
// erase(elem);//删除容器中值为 elem 的元素。
s1.erase(s1.begin());
s1.erase();
PrintSet(s1);
cout << "----------------------" << endl;
} // set查找
// find(key);//查找键 key 是否存在,若存在,返回该键的元素的迭代器;若不存在,返回 map.end();
// lower_bound(keyElem);//返回第一个 key>=keyElem 元素的迭代器。
// upper_bound(keyElem);//返回第一个 key>keyElem 元素的迭代器。
// equal_range(keyElem);//返回容器中 key 与 keyElem 相等的上下限的两个迭代器。
void test02()
{
set<int> s1;
s1.insert();
s1.insert();
s1.insert();
s1.insert();
s1.insert();
set<int>::iterator ret = s1.find();
if (ret == s1.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "ret: " << *ret << endl;
}
// 找第一个大于key的值
ret = s1.upper_bound();
if (ret == s1.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "ret: " << *ret << endl;
}
// equal_range 返回Lower_bound 和 upper_bound值
pair<set<int>::iterator, set<int>::iterator> myret = s1.equal_range();
if (myret.first == s1.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "myret: " << *(myret.first) << endl;
}
if (myret.second == s1.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "myret: " << *(myret.second) << endl;
}
cout << "----------------" << endl;
} class Person
{
public:
Person(int age, int id) :id(id), age(age){}
public:
int id;
int age;
}; class mycompare2
{
public:
bool operator()(Person p1, Person p2)
{
if (p1.id == p2.id)
{
return p1.age > p2.age;
}
else
{
p1.id > p2.id;
}
}
}; void test03()
{
set<Person, mycompare2> sp;
Person p1(, ), p2(, ), p3(, );
sp.insert(p1);
sp.insert(p2);
sp.insert(p3);
Person p4(, );
for (set<Person, mycompare2>::iterator it = sp.begin(); it != sp.end(); it++)
{
cout << (*it).age << " " << (*it).id << endl;
}
set<Person, mycompare2>::iterator ret = sp.find(p4);
if (ret == sp.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到:" << (*ret).id << " " << (*ret).age << endl;
}
} // 对组 void test04()
{
// 构造方法
pair<int, int> pair1(, );
cout << pair1.first << " " << pair1.second << endl;
pair<int, string> pair2 = make_pair(, "aaaaa");
cout << pair2.first << " " << pair2.second << endl;
pair<int, string> pair3 = pair2;
} int main()
{
test01();
test02();
test03();
test04();
getchar();
return ;
}

C++ STL 之 set 和 pair的更多相关文章

  1. STL之map与pair与unordered_map常用函数详解

    STL之map与pair与unordered_map常用函数详解 一.map的概述 map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称 ...

  2. stl(set和pair)

    D - 4 Gym - 100989D In this cafeteria, the N tables are all ordered in one line, where table number ...

  3. UVA 10763 Foreign Exchange 出国交换 pair+map

    题意:给出很多对数字,看看每一对(a,b)能不能找到对应的(b,a). 放在贪心这其实有点像检索. 用stl做,map+pair. 记录每一对出现的次数,然后遍历看看对应的那一对出现的次数有没有和自己 ...

  4. make_pair

    Utilities <utility> 由短小精干的类和函数构成,执行最一般性的工作. 这些工具包括: general types 一些重要的C函数 numeric limits Pair ...

  5. Codeforces Round 500 (Div 2) Solution

    从这里开始 题目地址 瞎扯 Problem A Piles With Stones Problem B And Problem C Photo of The Sky Problem D Chemica ...

  6. 【转】c++ make_pair函数使用

    [好记性不如烂笔头:在<C++ Templates>看到这个函数,发现正是前段时间写项目程序所要用到的,可惜当时还不知道有这个用法,当时是自己写了个结构体..]Utilities < ...

  7. C++11--Tuple类<tuple>

    #include "stdafx.h" #include <iomanip> #include <condition_variable> #include ...

  8. 洛谷 P1954 [NOI2010]航空管制

    https://www.luogu.org/problemnew/show/P1954 拓扑排序, 注意到如果正着建图("a出现早于b"=>"a向b连边" ...

  9. STL的pair学习, map学习

    http://blog.csdn.net/calvin_zcx/article/details/6072286 http://www.linuxidc.com/Linux/2014-10/107621 ...

随机推荐

  1. jeecg启动报错“com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.”的解决办法

    在运行"maven build"-->"tomcat:run"之后,报如下错误: com.mysql.jdbc.exceptions.jdbc4.MySQ ...

  2. C-LODOP的端口和网站的端口

    c-lodop的端口和网站的端口是不同的,不需要修改自己网站的端口.c-lodop32位标准版端口:8000,18000 (http网站)c-lodop32扩展版端口:8000,18000(http网 ...

  3. django的邮件email功能

    注意 测试的时候python manage.py test -p "test_tasks.py" -v 3,默认使用的EMAIL_BACKEND配置为:'django.core.m ...

  4. intel 酷睿core系列cpu的类型:U M H HQ MQ

    相对于笔记本来说.一般我们说的intel系列cpu是指应用于desktop桌面版,embedded嵌入式版, mobile移动版 桌面版和移动版cpu对比 http://tieba.baidu.com ...

  5. 安卓运行linux应用程序

    安卓是可以运行linux应用程序的,安卓系统原来就基于Linux.但是安卓已经把linux改头换面了.具体方法是安装Termux软件,然后就可以运行pkg命令安装软件包了,希望可以帮助到大家.

  6. kafka的offset相关知识

    Offset存储模型 由于一个partition只能固定的交给一个消费者组中的一个消费者消费,因此Kafka保存offset时并不直接为每个消费者保存,而是以 groupid-topic-partit ...

  7. [github] 关于华为鸿蒙OS

    English Docs | 中文文档 | Türkçe Dökümanlar HarmonyOS Ⅰ. 鸿蒙系统简介 鸿蒙系统(HarmonyOS),是第一款基于微内核的全场景分布式OS,是华为自主 ...

  8. SQL SERVER 使用游标删除所有主键

    Declare @Pk varChar(100);Declare @TBname varChar(100);declare cursor1 cursor for Select Sys2.name as ...

  9. 什么是SSL证书服务?

    SSL证书服务(Alibaba Cloud SSL Certificates Service)由阿里云联合多家国内外数字证书管理和颁发的权威机构.在阿里云平台上直接提供的服务器数字证书.您可以在阿里云 ...

  10. HADR和TSA需要注意的问题

    1.必须将主备数据库都执行一次完全备份,然后使用主库执行online备份后同步至备库: 2.同一台服务器只能添加一个TSA集群管理器,此服务器中无论有几个实例和几个数据库,都会被添加至首次创建的集群中 ...