热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

STL学习set

简介map是键-值的集合,而set是单纯的键的集合。set不支持下标操作,value_type不是pair类型而是于key_type相同的类型。set底

简介


map是键-值的集合,而set是单纯的键的集合。set不支持下标操作,value_type不是pair类型而是于key_type相同的类型。set底层实现为RB-Tree。




特性


set根据键值自动排序。
set中键值是唯一不重复的。
set的迭代器在STL中被定义为const_iterator,因此不能通过迭代器来改变键值。
set的插入和删除操作只影响当前键值迭代器,其他迭代器仍然有效。



常用操作

s.count(k) 返回 s 中元素 k 出现的次数
s.find(k) 如果 s 中存在元素 k 则返回指向元素的迭代器,若不存在,则返回超出末端容器
s.insert(e) 将元素 e 插入到 s 中。返回值类型为std::pair,其中iterator代表插入位置,
bool代表插入是否成功
s.inset(beg, end) beg 和 end 是标记元素范围的迭代器,将beg 到 end 之间的元素插入到s中,返回值为void
s.insert(iter, e) 将元素e插入到s中的iter位置,返回一个迭代器,指向插入元素位置
s.erase(k) 删除s中元素 k , 返回删除元素的个数
s.erase(p) 从s中删除迭代器p指向的元素, 返回void
s.erase(beg, end) 从s中删除由迭代器beg到end的所有元素,返回void



源码(STL3.3)


/*** Copyright (c) 1994* Hewlett-Packard Company** Permission to use, copy, modify, distribute and sell this software* and its documentation for any purpose is hereby granted without fee,* provided that the above copyright notice appear in all copies and* that both that copyright notice and this permission notice appear* in supporting documentation. Hewlett-Packard Company makes no* representations about the suitability of this software for any* purpose. It is provided "as is" without express or implied warranty.*** Copyright (c) 1996,1997* Silicon Graphics Computer Systems, Inc.** Permission to use, copy, modify, distribute and sell this software* and its documentation for any purpose is hereby granted without fee,* provided that the above copyright notice appear in all copies and* that both that copyright notice and this permission notice appear* in supporting documentation. Silicon Graphics makes no* representations about the suitability of this software for any* purpose. It is provided "as is" without express or implied warranty.*//* NOTE: This is an internal header file, included by other STL headers.* You should not attempt to use it directly.*/#ifndef __SGI_STL_INTERNAL_SET_H
#define __SGI_STL_INTERNAL_SET_H#include __STL_BEGIN_NAMESPACE#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma set woff 1174
#pragma set woff 1375
#endif// Forward declarations of operators ),class _Alloc = __STL_DEFAULT_ALLOCATOR(_Key) >
class set;template
inline bool operator&#61;&#61;(const set<_Key,_Compare,_Alloc>& __x, const set<_Key,_Compare,_Alloc>& __y);template
inline bool operator<(const set<_Key,_Compare,_Alloc>& __x, const set<_Key,_Compare,_Alloc>& __y);template
class set {// requirements:__STL_CLASS_REQUIRES(_Key, _Assignable);__STL_CLASS_BINARY_FUNCTION_CHECK(_Compare, bool, _Key, _Key);public:// typedefs:typedef _Key key_type;typedef _Key value_type;typedef _Compare key_compare;typedef _Compare value_compare;
private:typedef _Rb_tree, key_compare, _Alloc> _Rep_type;_Rep_type _M_t; // red-black tree representing set
public:typedef typename _Rep_type::const_pointer pointer;typedef typename _Rep_type::const_pointer const_pointer;typedef typename _Rep_type::const_reference reference;typedef typename _Rep_type::const_reference const_reference;typedef typename _Rep_type::const_iterator iterator;typedef typename _Rep_type::const_iterator const_iterator;typedef typename _Rep_type::const_reverse_iterator reverse_iterator;typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;typedef typename _Rep_type::size_type size_type;typedef typename _Rep_type::difference_type difference_type;typedef typename _Rep_type::allocator_type allocator_type;// allocation/deallocationset() : _M_t(_Compare(), allocator_type()) {}explicit set(const _Compare& __comp,const allocator_type& __a &#61; allocator_type()): _M_t(__comp, __a) {}#ifdef __STL_MEMBER_TEMPLATEStemplate set(_InputIterator __first, _InputIterator __last): _M_t(_Compare(), allocator_type()){ _M_t.insert_unique(__first, __last); }template set(_InputIterator __first, _InputIterator __last, const _Compare& __comp,const allocator_type& __a &#61; allocator_type()): _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
#elseset(const value_type* __first, const value_type* __last) : _M_t(_Compare(), allocator_type()) { _M_t.insert_unique(__first, __last); }set(const value_type* __first, const value_type* __last, const _Compare& __comp,const allocator_type& __a &#61; allocator_type()): _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }set(const_iterator __first, const_iterator __last): _M_t(_Compare(), allocator_type()) { _M_t.insert_unique(__first, __last); }set(const_iterator __first, const_iterator __last, const _Compare& __comp,const allocator_type& __a &#61; allocator_type()): _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
#endif /* __STL_MEMBER_TEMPLATES */set(const set<_Key,_Compare,_Alloc>& __x) : _M_t(__x._M_t) {}set<_Key,_Compare,_Alloc>& operator&#61;(const set<_Key, _Compare, _Alloc>& __x){ _M_t &#61; __x._M_t; return *this;}// accessors:key_compare key_comp() const { return _M_t.key_comp(); }value_compare value_comp() const { return _M_t.key_comp(); }allocator_type get_allocator() const { return _M_t.get_allocator(); }iterator begin() const { return _M_t.begin(); }iterator end() const { return _M_t.end(); }reverse_iterator rbegin() const { return _M_t.rbegin(); } reverse_iterator rend() const { return _M_t.rend(); }bool empty() const { return _M_t.empty(); }size_type size() const { return _M_t.size(); }size_type max_size() const { return _M_t.max_size(); }void swap(set<_Key,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); }// insert/erasepair insert(const value_type& __x) { pair __p &#61; _M_t.insert_unique(__x); return pair(__p.first, __p.second);}iterator insert(iterator __position, const value_type& __x) {typedef typename _Rep_type::iterator _Rep_iterator;return _M_t.insert_unique((_Rep_iterator&)__position, __x);}
#ifdef __STL_MEMBER_TEMPLATEStemplate void insert(_InputIterator __first, _InputIterator __last) {_M_t.insert_unique(__first, __last);}
#elsevoid insert(const_iterator __first, const_iterator __last) {_M_t.insert_unique(__first, __last);}void insert(const value_type* __first, const value_type* __last) {_M_t.insert_unique(__first, __last);}
#endif /* __STL_MEMBER_TEMPLATES */void erase(iterator __position) { typedef typename _Rep_type::iterator _Rep_iterator;_M_t.erase((_Rep_iterator&)__position); }size_type erase(const key_type& __x) { return _M_t.erase(__x); }void erase(iterator __first, iterator __last) { typedef typename _Rep_type::iterator _Rep_iterator;_M_t.erase((_Rep_iterator&)__first, (_Rep_iterator&)__last); }void clear() { _M_t.clear(); }// set operations:iterator find(const key_type& __x) const { return _M_t.find(__x); }size_type count(const key_type& __x) const {return _M_t.find(__x) &#61;&#61; _M_t.end() ? 0 : 1;}iterator lower_bound(const key_type& __x) const {return _M_t.lower_bound(__x);}iterator upper_bound(const key_type& __x) const {return _M_t.upper_bound(__x); }pair equal_range(const key_type& __x) const {return _M_t.equal_range(__x);}#ifdef __STL_TEMPLATE_FRIENDStemplate friend bool operator&#61;&#61; (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);template friend bool operator<(const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
#else /* __STL_TEMPLATE_FRIENDS */friend bool __STD_QUALIFIERoperator&#61;&#61; __STL_NULL_TMPL_ARGS (const set&, const set&);friend bool __STD_QUALIFIERoperator<__STL_NULL_TMPL_ARGS (const set&, const set&);
#endif /* __STL_TEMPLATE_FRIENDS */
};template
inline bool operator&#61;&#61;(const set<_Key,_Compare,_Alloc>& __x, const set<_Key,_Compare,_Alloc>& __y) {return __x._M_t &#61;&#61; __y._M_t;
}template
inline bool operator<(const set<_Key,_Compare,_Alloc>& __x, const set<_Key,_Compare,_Alloc>& __y) {return __x._M_t <__y._M_t;
}#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDERtemplate
inline bool operator!&#61;(const set<_Key,_Compare,_Alloc>& __x, const set<_Key,_Compare,_Alloc>& __y) {return !(__x &#61;&#61; __y);
}template
inline bool operator>(const set<_Key,_Compare,_Alloc>& __x, const set<_Key,_Compare,_Alloc>& __y) {return __y <__x;
}template
inline bool operator<&#61;(const set<_Key,_Compare,_Alloc>& __x, const set<_Key,_Compare,_Alloc>& __y) {return !(__y <__x);
}template
inline bool operator>&#61;(const set<_Key,_Compare,_Alloc>& __x, const set<_Key,_Compare,_Alloc>& __y) {return !(__x <__y);
}template
inline void swap(set<_Key,_Compare,_Alloc>& __x, set<_Key,_Compare,_Alloc>& __y) {__x.swap(__y);
}#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM !&#61; _MIPS_SIM_ABI32)
#pragma reset woff 1174
#pragma reset woff 1375
#endif__STL_END_NAMESPACE#endif /* __SGI_STL_INTERNAL_SET_H */// Local Variables:
// mode:C&#43;&#43;
// End:




stl_set.h

推荐阅读
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • node.jsrequire和ES6导入导出的区别原 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了解决二叉树层序创建问题的方法。通过使用队列结构体和二叉树结构体,实现了入队和出队操作,并提供了判断队列是否为空的函数。详细介绍了解决该问题的步骤和流程。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • 标题: ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • C++语言入门:数组的基本知识和应用领域
    本文介绍了C++语言的基本知识和应用领域,包括C++语言与Python语言的区别、C++语言的结构化特点、关键字和控制语句的使用、运算符的种类和表达式的灵活性、各种数据类型的运算以及指针概念的引入。同时,还探讨了C++语言在代码效率方面的优势和与汇编语言的比较。对于想要学习C++语言的初学者来说,本文提供了一个简洁而全面的入门指南。 ... [详细]
  • 本文介绍了H5游戏性能优化和调试技巧,包括从问题表象出发进行优化、排除外部问题导致的卡顿、帧率设定、减少drawcall的方法、UI优化和图集渲染等八个理念。对于游戏程序员来说,解决游戏性能问题是一个关键的任务,本文提供了一些有用的参考价值。摘要长度为183字。 ... [详细]
  • 本文介绍了Python函数的定义与调用的方法,以及函数的作用,包括增强代码的可读性和重用性。文章详细解释了函数的定义与调用的语法和规则,以及函数的参数和返回值的用法。同时,还介绍了函数返回值的多种情况和多个值的返回方式。通过学习本文,读者可以更好地理解和使用Python函数,提高代码的可读性和重用性。 ... [详细]
  • STL迭代器的种类及其功能介绍
    本文介绍了标准模板库(STL)定义的五种迭代器的种类和功能。通过图表展示了这几种迭代器之间的关系,并详细描述了各个迭代器的功能和使用方法。其中,输入迭代器用于从容器中读取元素,输出迭代器用于向容器中写入元素,正向迭代器是输入迭代器和输出迭代器的组合。本文的目的是帮助读者更好地理解STL迭代器的使用方法和特点。 ... [详细]
author-avatar
416703721
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有