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. IT诗词

    年少太轻狂,误入IT行.白发森森立,两眼直茫茫.语言数十种,无一称擅长.三十而立时,无房单身郎. 年少不经事,埋头编程忙. 指键铿锵落,不及细思量. bug千百个,comment无一行. 休言敏捷易, ...

  2. appium+python自动化34-获取元素属性get_attribute

    获取text # coding:utf-8 from appium import webdriver from time import sleep desired_caps = { 'platform ...

  3. java代码-----运用endWith()和start()方法

    总结: package com.a.b; //startWith().和endWith()是检查一个字符串是否以一个特定的字符序列开始或结束 public class Sdfs { public st ...

  4. ActiveMQ入门之四--ActiveMQ持久化方式

    消息持久性对于可靠消息传递来说应该是一种比较好的方法,有了消息持久化,即使发送者和接受者不是同时在线或者消息中心在发送者发送消息后宕机了,在消息中心重新启动后仍然可以将消息发送出去,如果把这种持久化和 ...

  5. select函数源码阅读

    fd_set结构体 #undef __NFDBITS /* It's easier to assume 8-bit bytes than to get CHAR_BIT. */ #define __N ...

  6. Windows下安装GCC

    1.GCC编译器的选择 Windows下最常见的安装GCC的方式有两种:Cygwin和MinGW.本文主要介绍MinGW的安装配置. 2.下载MinGW 下载地址:http://sourceforge ...

  7. 强大的NCBI接口

    刚才小玩了下,不错,.net确实很方便,很强大 Using Entrez Utilities Web Service with C# and MS Visual Studio 2005 Updated ...

  8. 关于在Arduino中调用DS1302模块

    DS1302时钟模块中的电池是起掉电保存作用的,在实际运行中必须给他的GND和VCC供电,否则得到的是错误的时间. 也就是说,电池是保存日期的,而无法提供芯片正常运行所需的电力. 从芯片引脚上可以看出 ...

  9. WPF Grid 用 C# 代码后台设置

    WPF Grid 用 C# 代码后台设置 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-21 参考: System.Wind ...

  10. STL : 反向迭代器(Reverse Iterator)

    1. 定义反向迭代器(Reverse Iterator)是一种反向遍历容器的迭代器.也就是,从最后一个元素到第一个元素遍历容器.反向迭代器将自增(和自减)的含义反过来了:对于反向迭代器,++运算将访问 ...