STL--集和多集(set/multiset)

与基本容器相比,关联容器更注重快速和高效地检索数据的能力。这些容器是根据键值(key)来检索数据的,键可以是值也可以是容器中的某一成员。这一类中的成员在初始化后都是按一定顺序排好序的。
本文地址:http://www.cnblogs.com/archimedes/p/cpp-set-multiset.html ,转载请注明源地址。
set和multiset 容器类(集和多集):#include<set>
<set> 内部它实现: 红黑树
插入删除查找复杂度log(n)
其中所包含的元素的值是唯一的(map)
<multiset> 允许重复元素
集合(set)是一个容器,它其中所包含的元素的值是唯一的。这在收集一个数据的具体值的时候是有用的。集合中的元素按一定的顺序排列,并被作为集合中的实例。如果你需要一个键/值对(pair)来存储数据,map(也是一个关联容器,后面将马上要讲到)是一个更好的选择。一个集合通过一个链表来组织,在插入操作和删除操作上比向量(vector)快,但查找或添加末尾的元素时会有些慢。
在集合中,所有的成员都是排列好的。如果先后往一个集中插入:12,2,3,123,5,65
则输出该集合为:2,3,5,12,65,123
集合(set)与多集(multiset)的区别是:set支持唯一键值,set中的值都是特定的,而且只出现一次;而multiset中可以出现副本键,同一值可以出现多次。
set和multiset的模板参数
template<class key, class compare, class allocator=allocator>
  • 第一个参数key是所存储的键的类型
  • 第二个参数是为排序值而定义的比较函数的类型
  • 第三个参数是被实现的存储分配符的类型。
在有些编译器的具体实现中,第三个参数可以省略。第二个参数使用了合适形式的迭代器为键定义了特定的关系操作符,并用来在容器中遍历值时建立顺序。集合的迭代器是双向,同时也是常量的,所以迭代器在使用的时候不能修改元素的值。
set定义了三个构造函数:
1、默认构造函数
explicit set(const Compare&=compare());
如:set<int,less<int> > set1;
less<int>是一个标准类,用于形成降序排列函数对象。升序排列是用greater<int>。
2、通过指定某一预先定义的区间来初始化set对象的构造函数
template<class InputIterator> set(InputIterator, InputIterator, const Compare&=compare());
如:set<int ,less<int> >set2(vector1.begin(),vector1.end());
3、复制构造函数
set(const set<Key,Compare&>);
如:set<int ,less<int>>set3(set2);
 
set容器详解:
头文件  #include <set>
定义变量  set <int> myset;
主要成员函数:
  • myset.insert(elem)   向集合中插入数据,如果已经存在则不插入
  • myset.erase(elem)      删除集合中值等于 elem的元素
  • myset.find(elem)       查找值等于elem的元素,若找到返回指向elem的迭代器,否则返回end() ,
  • myset.clear()  清除集合中所有数据
  • myset.size()  返回集合中数据个数
multiset操作详解:
头文件  #include <set>
定义变量  multiset <int> mymulset;
主要成员函数
  • mymulset.insert(elem)   向多重集合中插入数据,
  • mymulset.erase(elem)  删除多重集合中值等于 elem的所有元素,若删除成功返回删除个数,否则返回0
  • mymulset.count(elem)  返回多重集合中数据elem出现的次数
multiset用法:
#include <set>
using namespace std;

struct SS {int x,y;};
struct ltstr { 
    bool operator() (SS a, SS b)
    {return a.x < b.x;}  
};
int main() 
{ 
    set <SS, ltstr> st; // st内的SS元素按x从小到大排序
    …
}
#include <set>
using namespace std;
struct SS {
    int x,y;
    bool operator < (struct SS _s) const {
        if (x == _s.x) return y < _s.y;
        return x < _s.x;
    }
};
int main() 
{ 
    set <SS> st; // st内的SS元素按x从小到大排序
       …
}

multiset举例:

#include <iostream>
#include <set>
using namespace std;
int main(void) {
    set<int> set1;
    for (int i = 0; i < 10; ++i)  set1.insert(i);
    for (set<int>::iterator p = set1.begin();p != set1.end();++p) cout << *p << "";
    if (set1.insert(3).second)//把3插入到set1中
//插入成功则set1.insert(3).second返回true,否则返回false
//此例中,集中已经有3这个元素了,所以插入将失败
        cout << "set insert success";
    else
        cout << "set insert failed";
    if (set1.find(3) != set1.end()) { // 查找元素3
        cout << "find it.." << endl;
    } else {
        cout << "not find it.." << endl;
    }
if (set1.find(300) != set1.end()) { // 查找元素100
        cout << "find it.." << endl;
    } else {
        cout << "not find it.." << endl;
    }
    int a[] = {4, 1, 1, 1, 1, 1, 0, 5, 1, 0};
    multiset<int> A;
    A.insert(set1.begin(), set1.end());
    A.insert(a, a + 10);
    cout << endl;
    for (multiset<int>::iterator p = A.begin();p != A.end();++p)
        cout << *p << " ";
    cin.get();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值