集合与多重集合(set和multiset)
来源:互联网 发布:台达dvp14ss编程软件 编辑:程序博客网 时间:2024/06/06 04:44
集合与多重集合(set和multiset)
与基本容器相比,关联容器更注重快速和高效地检索数据的能力。这些容器是根据键值(key)来检索数据的,键可以是值也可以是容器中的某一成员。这一类中的成员在初始化后都是按一定顺序排好序的。
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定义了三个构造函数
默认构造函数
explicit set(const Compare&=compare());
如:set<int,less<int> > set1;
less<int>是一个标准类,用于形成降序排列函数对象。升序排列是用greater<int>。
通过指定某一预先定义的区间来初始化set对象的构造函数
template<class InputIterator> set(InputIterator, InputIterator, const Compare&=compare());
如:set<int ,less<int> >set2(vector1.begin(),vector1.end());
复制构造函数
set(const set<Key,Compare&>);
如:set<int ,less<int>>set3(set2);
注意:默认构造是降序排列。set<int> set1;也是可以的,默认为降序。另外,如果用无序的向量来初始化set对象,也是默认降序排列。
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与set是在同一个头文件
定义变量 multiset <int> mymulset;
主要成员函数
mymulset.insert(elem) 向多重集合中插入数据,
mymulset.erase(elem) 删除多重集合中值等于elem的所有元素, 若删除成功返回删除个数,否则返回0
mymulset.count(elem) 返回多重集合中数据elem出现的次数
实例
#include <iostream>#include <set> // 集合#include <vector>using namespace std; int main(){ 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 << " "; cout << endl; if (set1.insert(3).second) cout << "set insert success" <<endl; //把3插入到set1中 //插入成功则set1.insert(3).second返回true,否则返回false //此例中,集中已经有3这个元素了, 所以插入将失败 else cout << "set insert failed" <<endl; // 查找元素3 if (set1.find(3) != set1.end()) { cout << "find it.." << endl; } else { cout << "not find it.." << endl; } // 查找元素100 if (set1.find(300) != set1.end()) { cout << "find it.." << endl; } else { cout << "not find it.." << endl; } // Multiset的初始化 int a[] = {4, 1, 1, 1, 1, 1, 0, 5, 1, 0}; multiset<int> A; A.insert(a, a + 10); // 默认按降序排列 for (multiset<int>::iterator q = A.begin();q != A.end();++q) cout << *q << " "; cout << endl; getchar(); return 0;}
- 集合与多重集合(set和multiset)
- multiset 多重集合容器
- Multiset多重集合容器
- STL多重集合multiset
- multiset多重集合容器
- multiset多重集合容器
- multiset 多重集合容器
- STL(十二)multiset多重集合容器
- [STL基础]multiset多重集合
- C++STL之multiset多重集合容器
- 【C++】STL常用容器总结之十:多重映射multimap和多重集合multiset
- STL学习笔记-set/multiset容器(集合)
- Codeforces 527C Glass Carving<set集合和multiset集合的使用>
- 51nod 1672 区间交 (优先队列priority_queue 或 多重集合multiset)
- C++ STL学习笔记八 multiset多重集合容器
- STL泛型编程-multiset多重集合容器
- multiset 多重集合容器 C++容器 学习笔记
- C++ STL学习笔记八 multiset多重集合容器
- 操作系统学习笔记——保护模式——分段机制
- hdu-5104
- 青い文学 走れメロス 台詞
- 实用的Linux/Unix快捷键
- 谈谈初学者该怎么学电脑
- 集合与多重集合(set和multiset)
- js获取div的坐标
- WIFI破解之胡言乱语
- UVALive 6198 A Terribly Grimm Problem 二分匹配 求字典序最小解
- CodeForces 34C Page Numbers
- Xcode设置项之Architectures和Valid Architectures
- hibernate简单实例
- 映射和多重映射(map和multimap)
- String Date Calendar Timestamp之间的转换