c++ 数据结构之 set
来源:互联网 发布:中国软件协会培训中心 编辑:程序博客网 时间:2024/05/16 09:32
标准模板库STL中提供的set容器。
set是一个容器,它可以存储数据,读取数据,但每个值必须唯一,可以使用multiset储存重复的数据。系统根据值,对数据自动排序,每个值不能被直接改变。
#include <set>#using namespace std;模版原型
template<class Key, 储存在set中的关键字类型,即KEY的类型
class Compare=lcss<Key>, 提供比较两个元素的函数来决定在set中位置,是可选的,默认为lcss<Key>
class Allocator=allocator<Key>> 代笔储存管理设备,默认为allocator<Key>
set中的比较函数是默认的简单的比较,当使用特定的数据结构时,要重构比较函数:
class oper{public:bool operator()(string s1,string s2){//return true;returns1.length()<s2.length(); }};int _tmain(int argc, _TCHAR* argv[]){oper thisoper;set<string,oper> b(thisoper);b.insert("awff");b.insert("aaa");b.insert("ff");return 0;}
这时对于一个string 的set,将以字符串长短进行排序。
但是,这种在类中的比较只能用operator()以函数模式进行比较,但是只有这一个函数,只能进行排序,不能进行查找函数,应该还有去重构==操作符。若要重构<,==,>这些操作符,就必须,就必须在一个类中或struct里,以其本身为符号左边,对符号右边的对象进行大小判断。然后,我就要重新定义一个类或者struct作为key,然后使用这个重构的判断才能进行判断。
对于string类型,stl中可以直接进行默认的string判断操作,但是,要
#include <string>
set中比较重要的函数:
begin(),
返回指向首元素的定位器iterator.,定位器为双向的,指向Key和set,一般为iterator时,可以改变key值,若为const_iterator时,无法改变值.
对于set 的定位器,其地址指向的就是key的存放地址,所以*set就为key的值
oper thisoper;set<string,oper> b(thisoper);b.insert("awff");b.insert("aaa");b.insert("ff");set<string>::iterator p;p=b.begin();
clera();清空所有元素。void
count(<template> Key):返回对应Key的元素个数,一般时0或1,当对于multiset可能为其他值,返回int
oper thisoper;multiset<string,oper> b(thisoper);b.insert("awff");b.insert("ff");b.insert("ff");b.insert("ff");int n=b.count("ff");
empty():测试是否为空,返回bool
end():返回最后一个元素的定位器,iterator
这里的末尾定位器不储存数据,所以,当删除到遍历迭代器为end时,即对set完成了删除。
equal_range():返回一对定位器,一个指向>= 给定值的第一个Key,>给定值的第一个key。
oper thisoper;multiset<string,oper> b(thisoper);b.insert("awff");b.insert("ff");b.insert("ff");b.insert("ff");pair <set<string>::const_iterator,set<string>::const_iterator> p;p=b.equal_range("ff");结果一个指向awff,一个指向ff。
erase();将一定范围内的元素删除
find(template Key):找出与给定key相同的定位器,若不存在,指向set的结束定位器。
oper thisoper;multiset<string,oper> b(thisoper);b.insert("awff");b.insert("ff");b.insert("ff");b.insert("fff");set<string>::const_iterator p;p=b.find("ggg");判断的方法是由重定义比较函数规定的,所以,这里p指向的key为“fff”而不是"awff"。
get_allocator():返回set容器的一个allocator的副本
insert() 插入一个元素或一定数量的元素到应该的位置
oper thisoper;set<string,oper> b(thisoper);b.insert("awff");b.insert("ff");b.insert("gf");b.insert("g");b.insert(b.begin(),"fff");指定到位置后,set还会自己进行调整,导致位置恢复正常,所以,感觉没有多大意义去指定位置。对了,当是multiset时,可以对相同的key进行排序。还是有意义的。
ower_bound()
key_comp()
max_size()
rbegin()
rend()
size()
swap()
upper_bound() 返回大于某个值元素的迭代器
value_comp()
- c++ 数据结构之 set
- 数据结构set之HashSet
- 数据结构set之LinkedHashSet
- 数据结构之Set
- ES6之SET数据结构
- Python基础数据结构之set
- Python数据结构之集合Set
- Redis的数据结构之Set
- C++STL之set
- Python数据结构之——set
- STL map, STL set之数据结构基础
- STL MAP, STL SET之数据结构基础
- STL map, STL set之数据结构基础
- STL之map、set数据结构基础
- STL map, STL set之数据结构基础
- STL之map、set数据结构基础
- ECMAScript 6之Set和Map数据结构
- JavaScript之Set和Map数据结构
- ios开发中的基本设计模式
- 进程 内存查看
- Google support4 SlidingPaneLayout DrawerLayout
- JSTL标签库formatDate标签格式化日期
- JAVA基础3
- c++ 数据结构之 set
- CSDN主题首页
- hdu2222 AC自动机
- c++时间与java时间相互转化代码实例
- Solr4.6,搜索不区分大小写的配置
- 关于举办2014年全国高校IT骨干教师暑期培训班的通知
- apache +tomcat实现负载均衡
- 自定义BuufferedReader中的readLine方法。
- xe5 TStyleManager 界面风格