转载于:http://www.cnblogs.com/ChinaHook/p/6985444.html
转载不完全请原博客阅读。
1、结构
set和multiset会根据特定的排序原则将元素排序。两者不同之处在于,multisets允许元素重复,而set不允许重复。
只要是assignable、copyable、comparable(根据某个排序准则)的型别T,都可以成为set或者multisets的元素。如果没有特别的排序原则,采用默认的less,已operator < 对元素进行比较,以便完成排序。
排序准则必须遵守以下原则:
必须是“反对称的”,对operator <而言,如果x < y为真,y<x为假。
必须是“可传递的”,对operator <而言,如果x<y为真,y<z为真,那么x<z也为真。
必须是“非自反的”,对operator<而言,x<x永远为假。
2、能力
和所有的标准关联容器类似,sets和multisets通常以平衡二叉树完成。
自动排序的主要优点在于使二叉树搜寻元素具有良好的性能,在其搜索函数算法具有对数复杂度。但是自动排序也造成了一个限制,不能直接改变元素值,因为这样会打乱原有的顺序,要改变元素的值,必须先删除旧元素,再插入新元素。所以sets和multisets具有以下特点:
不提供直接用来存取元素的任何操作元素 通过迭代器进行元素的存取。
3、操作函数
3.1 构造、拷贝、析构
操作
效果
set c
产生一个空的set/multiset,不含任何元素
set c(op)
以op为排序准则,产生一个空的set/multiset
set c1(c2)
产生某个set/multiset的副本,所有元素都被拷贝
set c(beg,end)
以区间[beg,end)内的所有元素产生一个set/multiset
set c(beg,end, op)
以op为排序准则,区间[beg,end)内的元素产生一个set/multiset
c.~set()
销毁所有元素,释放内存
set<Elem>
产生一个set,以(operator <)为排序准则
set<Elem,0p>
产生一个set,以op为排序准则
3.2 非变动性操作
操作
效果
c.size()
返回当前的元素数量
c.empty ()
判断大小是否为零,等同于0 == size(),效率更高
c.max_size()
返回能容纳的元素最大数量
c1 == c2
判断c1是否等于c2
c1 != c2
判断c1是否不等于c2(等同于!(c1==c2))
c1 < c2
判断c1是否小于c2
c1 > c2
判断c1是否大于c2
c1 <= c2
判断c1是否小于等于c2(等同于!(c2<c1))
c1 >= c2
判断c1是否大于等于c2 (等同于!(c1<c2))
3.3 特殊的搜寻函数
sets和multisets在元素快速搜寻方面做了优化设计,提供了特殊的搜寻函数,所以应优先使用这些搜寻函数,可获得对数复杂度,而非STL的线性复杂度。比如在1000个元素搜寻,对数复杂度平均十次,而线性复杂度平均需要500次。
操作
效果
count (elem)
返回元素值为elem的个数
find(elem)
返回元素值为elem的第一个元素,如果没有返回end()
lower _bound(elem)
返回元素值为elem的第一个可安插位置,也就是元素值 >= elem的第一个元素位置
upper _bound (elem)
返回元素值为elem的最后一个可安插位置,也就是元素值 > elem 的第一个元素位置
equal_range (elem)
返回elem可安插的第一个位置和最后一个位置,也就是元素值==elem的区间
3.4 赋值
操作
效果
c1 = c2
将c2的元素全部给c1
c1.swap(c2)
将c1和c2 的元素互换
swap(c1,c2)
同上,全局函数
3.5 迭代器相关函数
sets和multisets的迭代器是双向迭代器,对迭代器操作而言,所有的元素都被视为常数,可以确保你不会人为改变元素值,从而打乱既定顺序,所以无法调用变动性算法,如remove()。
操作
效果
c.begin()
返回一个随机存取迭代器,指向第一个元素
c.end()
返回一个随机存取迭代器,指向最后一个元素的下一个位置
c.rbegin()
返回一个逆向迭代器,指向逆向迭代的第一个元素
c.rend()
返回一个逆向迭代器,指向逆向迭代的最后一个元素的下一个位置
3.6 安插和删除元素
必须保证参数有效,迭代器必须指向有效位置,序列起点不能位于终点之后,不能从空容器删除元素。
操作
效果
c.insert(elem)
插入一个elem副本,返回新元素位置,无论插入成功与否。
c.insert(pos, elem)
安插一个elem元素副本,返回新元素位置,pos为收索起点,提升插入速度。
c.insert(beg,end)
将区间[beg,end)所有的元素安插到c,无返回值。
c.erase(elem)
删除与elem相等的所有元素,返回被移除的元素个数。
c.erase(pos)
移除迭代器pos所指位置元素,无返回值。
c.erase(beg,end)
移除区间[beg,end)所有元素,无返回值。
c.clear()
移除所有元素,将容器清空
4、示例代码
4.1 set
// cont/set1.cpp
#include <iostream>
#include <set>
using namespace std;
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main()
{
</span><span style="color: #008000;">/*</span><span style="color: #008000;">type of the collection:
*-no duplicates
*-elements are integral values
*-descending order
</span><span style="color: #008000;">*/</span><span style="color: #000000;">
typedef </span><span style="color: #0000ff;">set</span><<span style="color: #0000ff;">int</span>,greater<<span style="color: #0000ff;">int</span>> ><span style="color: #000000;"> IntSet;
IntSet coll1; </span><span style="color: #008000;">//</span><span style="color: #008000;"> empty set container
</span><span style="color: #008000;">//</span><span style="color: #008000;">insert elements in random order</span>
coll1.insert(<span style="color: #800080;">4</span><span style="color: #000000;">);
coll1.insert(</span><span style="color: #800080;">3</span><span style="color: #000000;">);
coll1.insert(</span><span style="color: #800080;">5</span><span style="color: #000000;">);
coll1.insert(</span><span style="color: #800080;">1</span><span style="color: #000000;">);
coll1.insert(</span><span style="color: #800080;">6</span><span style="color: #000000;">);
coll1.insert(</span><span style="color: #800080;">2</span><span style="color: #000000;">);
coll1.insert(</span><span style="color: #800080;">5</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">iterate over all elements and print them</span>
IntSet::iterator pos; for (pos = coll1.begin(); pos != coll1.end(); ++pos) { cout << *pos << ’ '; } cout << endl;
</span><span style="color: #008000;">//</span><span style="color: #008000;">insert 4 again and process return value</span>
pair<IntSet::iterator,<span style="color: #0000ff;">bool</span>> status = coll1.insert(<span style="color: #800080;">4</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (status.second) {
cout </span><< <span style="color: #800000;">"</span><span style="color: #800000;">4 inserted as element </span><span style="color: #800000;">"</span>
<< distance (coll1.begin(),status. first) + <span style="color: #800080;">1</span>
<<<span style="color: #000000;"> endl;
}
</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
cout </span><< <span style="color: #800000;">"</span><span style="color: #800000;">4 already exists</span><span style="color: #800000;">"</span> <<<span style="color: #000000;"> endl;
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">assign elements to another set with ascending order</span>
<span style="color: #0000ff;">set</span><<span style="color: #0000ff;">int</span>><span style="color: #000000;"> coll2(coll1.begin(),
coll1.end());
</span><span style="color: #008000;">//</span><span style="color: #008000;">print all elements of the copy</span>
copy (coll2.begin(), coll2.end(), ostream_iterator<int>(cout," ")); cout << endl;
</span><span style="color: #008000;">//</span><span style="color: #008000;">remove all elements up to element with value 3</span>
coll2.erase (coll2.begin(), coll2.find(<span style="color: #800080;">3</span><span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;">remove all elements with value 5</span>
<span style="color: #0000ff;">int</span><span style="color: #000000;"> num;
num </span>= coll2.erase (<span style="color: #800080;">5</span><span style="color: #000000;">);
cout </span><< num << <span style="color: #800000;">"</span><span style="color: #800000;"> element(s) removed</span><span style="color: #800000;">"</span> <<<span style="color: #000000;"> endl;
</span><span style="color: #008000;">//</span><span style="color: #008000;">print all elements</span>
copy (coll2.begin(), coll2.end(), ostream_iterator<int>(cout," ")); cout << endl; }
输出:
6 5 4 3 2 1
4 already exists
1 2 3 4 5 6
1 element(s) removed
3 4 6
4.2 multiset
// cont/mset1.cpp
#include <iostream>
#include <set>
using namespace std;
int main() {
</span><span style="color: #008000;">/*</span><span style="color: #008000;">type of the collection:
*-duplicates allowed
*-elements are integral values
*-descending order
</span><span style="color: #008000;">*/</span><span style="color: #000000;">
typedef multiset</span><<span style="color: #0000ff;">int</span>,greater<<span style="color: #0000ff;">int</span>> ><span style="color: #000000;"> IntSet;
IntSet coll1, </span><span style="color: #008000;">//</span><span style="color: #008000;"> empty multiset container
</span><span style="color: #008000;">//</span><span style="color: #008000;">insert elements in random order</span>
coll1.insert(<span style="color: #800080;">4</span><span style="color: #000000;">);
coll1.insert(</span><span style="color: #800080;">3</span><span style="color: #000000;">);
coll1.insert(</span><span style="color: #800080;">5</span><span style="color: #000000;">);
coll1.insert(l);
coll1.insert(</span><span style="color: #800080;">6</span><span style="color: #000000;">);
coll1.insert(</span><span style="color: #800080;">2</span><span style="color: #000000;">);
coll1.insert(</span><span style="color: #800080;">5</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">iterate over all elements and print them</span>
IntSet::iterator pos; for (pos = coll1.begin(); pos != coll1.end(); ++pos) { cout << *pos << ’ '; } cout << endl;
</span><span style="color: #008000;">//</span><span style="color: #008000;">insert 4 again and process return value</span>
IntSet::iterator ipos = coll1.insert(<span style="color: #800080;">4</span><span style="color: #000000;">);
cout </span><< <span style="color: #800000;">"</span><span style="color: #800000;">4 inserted as element </span><span style="color: #800000;">"</span>
<< distance (coll1.begin(),ipos) + <span style="color: #800080;">1</span>
<<<span style="color: #000000;"> endl;
</span><span style="color: #008000;">//</span><span style="color: #008000;">assign elements to another multiset with ascending order</span>
multiset<<span style="color: #0000ff;">int</span>><span style="color: #000000;"> coll2(coll1.begin(),
coll1.end());
</span><span style="color: #008000;">//</span><span style="color: #008000;">print all elements of the copy</span>
copy (coll2.begin(), coll2.end(), ostream_iterator<int>(cout," ")); cout << endl;
</span><span style="color: #008000;">//</span><span style="color: #008000;">remove all elements up to element with value 3</span>
coll2.erase (coll2.begin(), coll2.find(<span style="color: #800080;">3</span><span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;">remove all elements with value 5</span>
<span style="color: #0000ff;">int</span><span style="color: #000000;"> num;
num </span>= coll2.erase (<span style="color: #800080;">5</span><span style="color: #000000;">);
cout </span><< num << <span style="color: #800000;">"</span><span style="color: #800000;"> element(s) removed</span><span style="color: #800000;">"</span> <<<span style="color: #000000;"> endl;
</span><span style="color: #008000;">//</span><span style="color: #008000;">print all elements</span>
copy (coll2.begin(), coll2.end(), ostream_iterator<int>(cout," ")); cout << endl; }
输出:
6 5 5 4 3 2 1
4 inserted as element 5
1 2 3 4 4 5 5 6
2 element(s) removed
3 4 4 6