4.2 set和multiset
使用必须包含头文件set
1)multiset
*:定义

如果不给第二个参数,默认less<key>,即用<来进行。
例如:
A是一个类的名字,则可以定义一个容器对象如下:
multiset<A>s;
由于multiset 的类型参数可以默认,所以上面的语句等价于:
multiset<int, less<A>,allocator<A>s>;

**:multiset的成员函数

find和count并不是通过==来进行比较值是否相等。它们的比较原则是x<y和y<x同时为假。
3)例
//program 19.4.2.1.cpp multiset的用法
#include <iostream>
#include <set> //使用multiset须包含此文件
using namespace std;
template <class T>
void Print(T first, T last)
{
for(;first != last ; ++first)
cout << * first << " ";
cout << endl;
}
class A
{
private:
int n;
public:
A(int n_ ) { n = n_; }
friend bool operator< ( const A & a1, const A & a2 )
{ return a1.n < a2.n; }
friend ostream & operator<< ( ostream & o, const A & a2 )
{ o << a2.n; return o; }
friend class MyLess;
};
class MyLess
{
public:
bool operator()( const A & a1, const A & a2) //按个位数比大小
{ return ( a1.n % ) < (a2.n % ); }
};
typedef multiset<A> MSET1; //MSET1用 "<"比较大小
typedef multiset<A,MyLess> MSET2; //MSET2用 MyLess::operator()比较大小
int main()
{
const int SIZE = ;
A a[SIZE] = { ,,,,, };
MSET1 m1;
m1.insert(a,a+SIZE);
m1.insert();
cout << "1) " << m1.count() << endl; //输出1) 2
cout << "2) "; Print(m1.begin(),m1.end()); //输出 2) 4 8 19 22 22 33 40
MSET1::iterator pp = m1.find();
if( pp != m1.end() ) //条件为真说明找到
cout << "found" << endl; //本行会被执行,输出 found
cout << "3) "; cout << * m1.lower_bound() << "," <<* m1.upper_bound()<< endl;
//输出 3) 22,33
pp = m1.erase(m1.lower_bound(),m1.upper_bound());//pp指向被删元素的下一个元素
cout << "4) "; Print(m1.begin(),m1.end()); //输出 4) 4 8 19 33 40
cout << "5) "; cout << * pp << endl; //输出 5) 33
MSET2 m2; // m2里的元素按n的个位数从小到大排
m2.insert(a,a+SIZE);
cout << "6) "; Print(m2.begin(),m2.end()); //输出 6) 40 22 33 4 8 19
return ;
}
2)set
*:定义
template<class Key, class Pred=less<Key>,class A=allocator<Key>
class set {....}
set和multiset相似,差别在于set中不能有重复元素。multiset的成员函数set也有,但由于没有重复元素,所以set中插入单个元素的insert成员函数和multiset不同,其原型如下,返回一个pair模板类的对象。
pair<iterator,bool>insert(const T & val);
假如set的insert成员函数的返回值是对象x,那么x.second为ture,则说明插入成功,此时x.first指向被插入元素的迭代器;x.second为false,说明已经有一个一样的值,插入失败,x.first指向原有那个元素的迭代器。
关联容器的另一个成员函数equal_range返回值也是pair模板类对象:
pair<iterator,iterator>equal_range(const T & val);
2)例
//program 19.4.3.1.cpp set的用法:
#include <iostream>
#include <set> //使用set须包含此文件
using namespace std;
int main()
{
typedef set<int>::iterator IT;
int a[] = { ,,,, };
set<int> st(a,a+); // st里是 1 2 3 4 6
pair< IT,bool> result;
result = st.insert(); // st变成 1 2 3 4 5 6
if( result.second ) //插入成功则输出被插入元素
cout << * result.first << " inserted" << endl; //输出: 5 inserted
if( st.insert().second )
cout << * result.first << endl;
else
cout << * result.first << " already exists" << endl; //输出 5 already exists
pair<IT,IT> bounds = st.equal_range();
cout << * bounds.first << "," << * bounds.second ; //输出:4,5
return ;
}
4.2 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可以存多个相同的 ...
- 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)是一种包含已排序对象的关联容器 ...
随机推荐
- ios中二维码的使用之一: 二维码的生成
iOS在7之后,具备了原生的二维码生成API; 生成二维码的准备: #import <CoreImage/CoreImage.h> 导入框架: 开始生成: //1- 创建过滤器 CIFi ...
- Opencv二值图像的分布直方图
Mat img; ]; int main() { VideoCapture video(); if (!video.isOpened()) { ; } Mat img; Mat img1, img2, ...
- 17 任务调度相关类综述——Live555源码阅读(一)任务调度相关类
这是Live555源码阅读的第二部分,包括了任务调度相关的三个类.任务调度是Live555源码中很重要的部分. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/ol ...
- Python自动化之多进程
多进程multiprocessing from multiprocessing import Process import os def info(title): print(title) print ...
- [转]mysql分布式方案-分库拆表
来源:http://kissthink.com/archive/mysql-distributed-programs---and-warehouses-split-table.html 分库& ...
- Android SDK在线更新镜像服务器
http://www.cnblogs.com/tc310/p/4696731.html#_label0
- php内核和瓦力上线部署
http://www.php-internals.com/ http://www.walle-web.io/
- ASP.NET 上的 Async/Await 简介
原文链接 大多数有关 async/await 的在线资源假定您正在开发客户端应用程序,但在服务器上有 async 的位置吗?可以非常肯定地回答“有”.本文是对 ASP.NET 上异步请求的概念性概述, ...
- c++ 引用和指针
1.引用 程序把引用和它的初始值绑定在一起,而不是将初始值拷贝给引用.一旦初始化完成,引用将和它的初始值对象一直绑定在一起.因为无法令引用重新绑定到另外一个对象,因此引用必须初始化. int ival ...
- ACM/ICPC 之 SPFA练习两道(ZOJ3088-ZOJ3103)
两道题都需要进行双向SPFA,比范例复杂,代码也较长,其中第二题应该可以用DFS或者BFS做,如果用DFS可能需要的剪枝较多. ZOJ3088-Easter Holydays //利用SPFA找出下降 ...