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的更多相关文章

  1. STL中的set/multiset小结

    (1)使用set/multiset之前必须包含头文件<set>:#include<set> (2)namespace std{ template <class T, cl ...

  2. STL中set和multiset小结

    (1)使用set/multiset之前必须包含头文件<set>:#include<set>    (2)namespace std{      template <cla ...

  3. STL中的set容器的一点总结

    1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...

  4. 【转】 STL中的set容器的一点总结

    1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...

  5. HDU 4022 Bombing(stl,map,multiset,iterater遍历)

    题目 参考了     1     2 #define _CRT_SECURE_NO_WARNINGS //用的是STL中的map 和 multiset 来做的,代码写起来比较简洁,也比较好容易理解. ...

  6. (转)STL中set的用法

    转载自here 1.关于set map容器是键-值对的集合,好比以人名为键的地址和电话号码.相反地,set容器只是单纯的键的集合.例如,某公司可能定义了一个名为bad_checks的set容器,用于记 ...

  7. STL中的set容器的一点总结(转)

    STL中的set容器的一点总结 1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂 ...

  8. STL中关于map和set的四个问题?

    STL map和set的使用虽不复杂,但也有一些不易理解的地方,如: 为何map和set的插入删除效率比用其他序列容器高? 或许有得人能回答出来大概原因,但要彻底明白,还需要了解STL的底层数据结构. ...

  9. STL中的set使用方法详细!!!!

    1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...

随机推荐

  1. Go - 类型与变量

    类型 Go 语言中的类型与其他语言类似,比较特殊的有以下几个: bool 类型 - 它的值只能是 true 与 false. int / uint - 它们的长度会根据操作系统的不同(32/64 bi ...

  2. 【语音识别】Microsoft Speech Platform 自学笔记2 环境要求与安装过程

    笔记人:又吹风 时 间:2012/12/16 主要内容:Microsoft Speech Platform的环境要求与安装过程. 上次也说过了,当前Microsoft Speech Platform最 ...

  3. UVA-11292Dragon of Loowater

    /* The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into ...

  4. [ Python ] Flask 基于 Web开发 大型程序的结构实例解析

    作为一个编程入门新手,Flask是我接触到的第一个Web框架.想要深入学习,就从<FlaskWeb开发:基于Python的Web应用开发实战>这本书入手,本书由于是翻译过来的中文版,理解起 ...

  5. springboot-shiro chapter02——springboot webmvc jsp

    简介:这一节主要涉及spring boot 支持jsp, 由于对spring boot不太熟悉,走了一些弯路. 环境:IDEA15+JDK1.8+Maven3+ 代码: https://git.osc ...

  6. 关于电机驱动扩展板 L293D 马达板Arduino

    注意端口3,4,5,6,7,8,9,10,11,12会被占用(板子上的pin口). 通过 MS_DCMotor motor(4); 中的4指的是4号电机,同理还有1-3号电机.不是pin口   舵机用 ...

  7. 前端使用CryptoJs类库进行sha-256、MD5加密

    Google的加密库 CryptoJs(点此下载) 包含了很多常用的加解密方式,包括AES.DES.SHA-1.SHA-2.SHA256.MD5等. DES对称加密在之前的文章中也有介绍过,大传送门. ...

  8. 【开发工具】Jenkins+Gitlab实现自动化部署

    我在尝试在容器中安装Jenkins时,初衷是希望使用docker in docker 的模式来实现Jenkins slave容器按需创建.在实现的时候需要在Jenkins 中安装Kubernetes插 ...

  9. bundle install 老是中断,可以在gemfile里面把source换成taobao源,可以自动安装

    bundle install 老是中断,可以在gemfile里面把source换成taobao源,可以自动安装

  10. 第六章 Validating with the Validation API

    CHAPTER 6 Validating with the Validation API Defining and Triggering Validation: An Overview 你可以使用以下 ...