STL中 set 和 multiset
1. 所在头文件: <set>, 命名空间: std ; 声明如下:
namespace std{
template <class T,
class Compare = less<T>,
class Allocator = allocator<T> >
class set;
template <class T,
class Compare = less<T>,
class Allocator = allocator<T> >
class multiset;
}
- 只要是assignable+copyable+comparable的类型T都可以作为它们的参数.
- 内部是用红黑树实现的.
- set不允许重复元素, 而multiset允许, 它们都不提供用来直接存取元素的任何操作函数.
- 通过迭代器进行元素间接存取, 有一个限制: 从迭代器角度看, 元素值是常数.
2. 基本操作
- set 和 multiset的构造/析构形式(6种):
set c;
set c(op); // op指定排序规则
set c1(c2);
set c(begin,end);
set c(begin,end,op)
c.~set()
- size()/empty()/max_size()/c1!=c2//c1==c2//c1<c2//c1>c2//c1<=c2//c1>=c2
- 要求有相同的排序准则. 不然会编译错误, 因为不是相同的类型.
- set和multiset的查询操作函数(是算法函数的特殊版本, 针对集合进行优化, 可以有对数而非线性效率)
- count(elem); //返回值为elem的元素个数
- find(elem); //返回元素值为elem的第一个元素位置, 否则返回end()
- lower_bound(elem); //返回elem的第一个可插入的位置, 也就是 元素值 >= elem元素值的第一个元素位置.
- upper_bound(elem); //和lower_bound()相对
- equal_range(elem);//返回的是pair值对
- 元素的插入和删除
- c.erase(elem/pos/[begin,end]);
- c.insert(elem/[pos,elem]/[begin,end]);
- c.clear();
- 如果multiset内含有重复元素, 就不能使用 erase()来删除第一个. 一般是用成员函数find()找到一个, 然后erase()
3. 实例:
set:
#include <iostream>
#include <iterator>
#include <set>
using namespace std;
int main(){
typedef set<int,greater<int> > IntSet;
IntSet coll1;
coll1.insert();
coll1.insert();
coll1.insert();
coll1.insert();
coll1.insert();
coll1.insert();
coll1.insert(); copy(coll1.begin(),coll1.end(),ostream_iterator<int>(cout," "));
cout<<endl; pair<IntSet::iterator,bool> status = coll1.insert();
if(status.second){
cout<<"4 is inserted"<<endl<<distance(coll1.begin(),status.first)+<<endl;
}else{
cout<<"4 is already existed!"<<endl;
coll1.insert();
} copy(coll1.begin(),coll1.end(),ostream_iterator<int>(cout," "));
cout<<endl; set<int> coll2(coll1.begin(),coll1.end());
copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
cout<<endl; coll2.erase(coll2.begin(),coll2.find()); copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
cout<<endl; cout<<coll2.erase()<<" elem of 5 is removed!"<<endl; copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
cout<<endl; return ;
}
multiset:
#include <iostream>
#include <iterator>
#include <set>
using namespace std;
int main(){
typedef multiset<int,greater<int> > IntSet;
IntSet coll1;
coll1.insert();
coll1.insert();
coll1.insert();
coll1.insert();
coll1.insert();
coll1.insert();
coll1.insert(); copy(coll1.begin(),coll1.end(),ostream_iterator<int>(cout," "));
cout<<endl; // 这里运行重复值, 所以没有返回失败状态.
IntSet::iterator pos = coll1.insert();
if(pos!=coll1.end()){
cout<<"4 is inserted"<<endl<<distance(coll1.begin(),pos)+<<endl;
}else{
cout<<"4 is can't be insert!"<<endl;
coll1.insert();
} copy(coll1.begin(),coll1.end(),ostream_iterator<int>(cout," "));
cout<<endl; multiset<int> coll2(coll1.begin(),coll1.end());
copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
cout<<endl; coll2.erase(coll2.begin(),coll2.find()); copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
cout<<endl; cout<<coll2.erase()<<" elem of 5 is removed!"<<endl; copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
cout<<endl; return ;
}
在执行期指定排序准则(默认是less<T>())
#include <iostream>
#include <iterator>
#include <set>
using namespace std;
template <class T>
class RuntimeCmp{
public:
enum cmp_mode {normal, reverse};
private:
cmp_mode mode; //在同一个类中定义了枚举, 然后定义变量.
public:
RuntimeCmp(cmp_mode m=normal): mode(m){}
bool operator()(const T& t1, const T& t2) const{
return mode == normal? t1<t2: t1>t2;
}
// 返回值给谁用?
bool operator == (const RuntimeCmp& rc){
return mode == rc.mode;
}
};
typedef set<int,RuntimeCmp<int> > IntSet;
void fill(IntSet & set){
set.insert();
set.insert();
set.insert();
set.insert();
set.insert();
set.insert();
set.insert();
}
int main(){
IntSet coll1;
fill(coll1);
copy(coll1.begin(),coll1.end(),ostream_iterator<int>(cout," "));
cout<<endl;
RuntimeCmp<int> reverse_order(RuntimeCmp<int>::reverse);
IntSet coll2(reverse_order);
fill(coll2);
copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
cout<<endl;
coll1 = coll2;
coll1.insert();
copy(coll1.begin(),coll1.end(),ostream_iterator<int>(cout," "));
cout<<endl;
//比较排序准则是否一致, 由于coll1 从coll2 拷贝而来, 所以一致.
cout<<(coll1.value_comp() == coll2.value_comp())<<endl; return ;
}
STL中 set 和 multiset的更多相关文章
- STL中的set/multiset小结
(1)使用set/multiset之前必须包含头文件<set>:#include<set> (2)namespace std{ template <class T, cl ...
- STL中set和multiset小结
(1)使用set/multiset之前必须包含头文件<set>:#include<set> (2)namespace std{ template <cla ...
- STL中的set容器的一点总结
1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...
- 【转】 STL中的set容器的一点总结
1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...
- HDU 4022 Bombing(stl,map,multiset,iterater遍历)
题目 参考了 1 2 #define _CRT_SECURE_NO_WARNINGS //用的是STL中的map 和 multiset 来做的,代码写起来比较简洁,也比较好容易理解. ...
- (转)STL中set的用法
转载自here 1.关于set map容器是键-值对的集合,好比以人名为键的地址和电话号码.相反地,set容器只是单纯的键的集合.例如,某公司可能定义了一个名为bad_checks的set容器,用于记 ...
- STL中的set容器的一点总结(转)
STL中的set容器的一点总结 1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂 ...
- STL中关于map和set的四个问题?
STL map和set的使用虽不复杂,但也有一些不易理解的地方,如: 为何map和set的插入删除效率比用其他序列容器高? 或许有得人能回答出来大概原因,但要彻底明白,还需要了解STL的底层数据结构. ...
- STL中的set使用方法详细!!!!
1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...
随机推荐
- Java程序员之Spring(一) 入门
一. Spring 原理讲解 Spring 是一个轻量容器框架(开源):Spring的核心是 IoC(控制反转) 和 AOP(面向切面编程): Spring 由7个模块组成: Spring Core ...
- scrapy_redis 实现多进程配置部分代码
# 启用Redis调度存储请求队列SCHEDULER = "scrapy_redis.scheduler.Scheduler"# 确保所有的爬虫通过Redis去重DUPEFILTE ...
- 关于Eclipse中复制粘贴一个项目后的操作
今天在做一个小Demo,内容和之前的项目有些类似就直接复制过来了,项目名修改了,web.xml的项目名也修改了,可是部署到Tomcat之后,以这个新项目名进行访问就会出现404的错误,只可以使用复制之 ...
- 【UVA】10935 Throwing cards away I(STL队列)
题目 题目 分析 练习STL 代码 #include <bits/stdc++.h> using namespace std; int main() { int n; wh ...
- 关于v$BH
关于v$bh的相关字段值FILE# NUMBER Datafile identifier number (to find the filename, query DBA_DATA_FILES or V ...
- 0007-一套完整的CRUD_DEMO
好久没写了,一直在忙别的东西,但是想想,还是把之前的补充完整好了.给大家一个参考,也为自己留个备份. 首先写一个Html作为内容载体,主要结构如下 <div ui-view="navb ...
- node启动appium.js
node启动appium.js,appium.js目录中不能有空格或者(x86)等字样
- 本地YUM仓库搭建实战
YUM主要用于自动安装.升级rpm软件包,它能自动查找并解决rpm包之间的依赖关系.要成功的使用YUM工具安装更新软件或系统,就需要有一个包含各种rpm软件包的repository(软件仓库),这个软 ...
- github上的版本发布
当前的版本号 发布版本 比如 git tag -a v1. 把这个版本发布到线上 git push --tags
- Spring高级话题
Spring Aware 在实际项目中,你不可避免的要用到spring容器本身的功能资源,这时你的bean要意识到spring容器的存在,才能调用spring提供的资源.spring aware本来就 ...