C++set 和 multiset的使用
最后一个自由支配的暑假,学一些自己感兴趣的部分,也算为大三作准备。
C++中set集合的使用
定义一个int类型的集合
set<int> s;
set<int>::iterator it;
基本操作有如下:
s.inert(10);//插入元素10
s.erase(10);//删除元素10
s.clear();//清空集合
s.size();//集合元素的个数
s.empty();//判断集合是否为空
it=s.find(10);//查找集合中是否有元素10,有的话返回10,没有返回s.end();
首先集合在数学的概念中就是互异性,不能有重复
需要注意的点:
1.是以中序遍历去遍历整个集合的,在插入的时候自动调整
2.遍历的时候需要判断一下集合是否为空;
3.插入的数默认从小到大排序 set<int>::iterator it;
4.如果要从大到小的话 set<int>::reverse_iterator rit;
for(rit=s.rbegin();rit!=s.rend();rit++)
{
cout<<*rit<<" ";
}
自定义比较函数,用到结构体
#include<set> #include<string> #include<iostream> using namespace std; struct Info { string name; float score; //重载 '<'操作符 bool operator < (const Info &a)const { //按照score从小到大排序 换为‘<’则为从大到小 return a.score>score; } }; int main() { set<Info> s; Info info; info.name="Jack"; info.score=; s.insert(info); info.name="Tom"; info.score=; s.insert(info); info.name="Nacy"; info.score=; s.insert(info); info.name="Elano"; info.score=; s.insert(info); set<Info>::iterator it; for(it=s.begin();it!=s.end();it++) cout<<(*it).name<<" : "<<(*it).score<<endl; return ; }
结果:
mutiset:多重集合 和set最大的区别就是,它可以插入重复的元素,
如果删除的话,相同的也一起删除了;
如果查找的话,返回该元素的迭代器的位置,若有相同,返回第一个元素的地址;
其他使用和set基本类似。
#include<set>
#include<string>
#include<iostream>
using namespace std;
int main()
{
//多重集合对象
multiset<string> ms;
ms.insert("abc");
ms.insert("");
ms.insert("") ;
ms.insert("aaa");
ms.insert("");
ms.insert("bbb"); multiset<string>::iterator it;
for(it=ms.begin();it!=ms.end();it++)
{
cout<<*it<<endl;
}
cout<<endl<<"集合的大小:"<<ms.size()<<endl; it=ms.find("");
if(it!=ms.end())
{
cout<<*it<<endl;
}
else cout<<"not found"<<endl; it=ms.find("");
if(it!=ms.end())
{
cout<<*it<<endl;
}
else cout<<"not found"<<endl; int n=ms.erase("");
cout<<"共删除:"<<n<<endl<<endl;
for(it=ms.begin();it!=ms.end();it++)
{
cout<<*it<<endl;
}
return ;
}
C++set 和 multiset的使用的更多相关文章
- C++ std::multiset
std::multiset template < class T, // multiset::key_type/value_type class Compare = less<T>, ...
- Guava学习笔记:Guava新增集合类型-Multiset
Guava引进了JDK里没有的,但是非常有用的一些新的集合类型.所有这些新集合类型都能和JDK里的集合平滑集成.Guava集合非常精准地实现了JDK定义的接口.Guava中定义的新集合有: Multi ...
- [Google Guava]学习--新集合类型Multiset
Guava提供了一个新集合类型Multiset,它可以多次添加相等的元素,且和元素顺序无关.Multiset继承于JDK的Cllection接口,而不是Set接口. Multiset主要方法介绍: a ...
- UVA11136Hoax or what( multiset的应用)
题目链接 题意:n天,每天往一个箱子里放m个数,放完之后取最大的Max和最小的min做差,并把这两个数去掉,求n天之后的和 multiset 和 set的原理是相似的,multiset可以存多个相同的 ...
- 4.2 set和multiset
使用必须包含头文件set 1)multiset *:定义 如果不给第二个参数,默认less<key>,即用<来进行. 例如: A是一个类的名字,则可以定义一个容器对象如下: mult ...
- STL(multiset) UVA 11020 Efficient Solutions
题目传送门 题意:训练指南P228 分析:照着书上的做法,把点插入后把它后面不占优势的点删除,S.size ()就是优势的人数,时间复杂度O (nlogn) #include <bits/std ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(可持久化Trie)
D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- STL中的set/multiset小结
(1)使用set/multiset之前必须包含头文件<set>:#include<set> (2)namespace std{ template <class T, cl ...
- STL--集和多集(set/multiset)
与基本容器相比,关联容器更注重快速和高效地检索数据的能力.这些容器是根据键值(key)来检索数据的,键可以是值也可以是容器中的某一成员.这一类中的成员在初始化后都是按一定顺序排好序的. 本文地址:ht ...
- C++ Set & MultiSet
转自http://www.cppblog.com/wanghaiguang/archive/2012/06/05/177627.html STL Set介绍集合(Set)是一种包含已排序对象的关联容器 ...
随机推荐
- codeforces 816 D. Karen and Test(逆元+思维+组合数)
题目链接:http://codeforces.com/contest/816/problem/D 题解:显然一看到这题应该会想到是有什么规律的于是多写几项就会发现偶数列之间是有关系的. 满足a[i][ ...
- hdu5491 The Next 模拟
Let LL denote the number of 1s in integer DD’s binary representation. Given two integers S1S1 and S2 ...
- yzoj P2043 & 洛谷 P1282 多米诺骨牌 题解
题意 类似于就是背包. 解析 代码 跟解析有点不一样v[i]价值,w[i]重量,s背包容积,背包转移即可. #include<bits/stdc++.h> using namespace ...
- 英文写作report
Writting Attached Files Maybe you might want to get familiar about how to write the Final report. ...
- Go依赖管理及Go module使用
Go语言的依赖管理随着版本的更迭正逐渐完善起来. 依赖管理 为什么需要依赖管理 最早的时候,Go所依赖的所有的第三方库都放在GOPATH这个目录下面.这就导致了同一个库只能保存一个版本的代码.如果不同 ...
- 049 模块6-wordcloud库的使用
目录 一.wordcloud库基本介绍 1.1 wordcloud库概述 1.2 wordcloud库的安装 二.wordcloud库使用说明 2.1 wordcloud库基本使用 2.2 wordc ...
- Linux上安装JDK1.7步骤
1.使用SecurtCRT连接上Linux,把jdk的压缩包传递过去:(传递的方法在我的博客中也有写,参考之前的博客) 2.解压缩jdk:tar -zxvf jdk-7u55-linux-i586.t ...
- Spring Cloud官方文档中文版-服务发现:Eureka客户端
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_spring_cloud_netflix 文中例子我做了一些测试在:h ...
- SpringBoot 2 快速整合 | Hibernate Validator 数据校验
概述 在开发RESTFull API 和普通的表单提交都需要对用户提交的数据进行校验,例如:用户姓名不能为空,年龄必须大于0 等等.这里我们主要说的是后台的校验,在 SpringBoot 中我们可以通 ...
- SpannableString设置文本背景色
参考内容: http://blog.csdn.net/harvic880925/article/details/38984705 http://blog.it985.com/14433.html 1. ...