C++ std::set
std::set
template < class T, // set::key_type/value_type
class Compare = less<T>, // set::key_compare/value_compare
class Alloc = allocator<T> // set::allocator_type
> class set;
Set
Sets are containers that store unique elements following a specific order.
In a set, the value of an element also identifies it (the value is itself the key, of type T), and each value must be unique. The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.
Internally, the elements in a set are always sorted following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).
set containers are generally slower than unordered set containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order.
Sets are typically implemented as binary search trees.
Container properties
- Associative Elements in associative containers are referenced by their key and not by their absolute position in the container.
- Ordered The elements in the container follow a strict order at all times. All inserted elements are given a position in this order.
- Set The value of an element is also the key used to identify it.
- Unique keys No two elements in the container can have equivalent keys.
- Allocator-aware The container uses an allocator object to dynamically handle its storage needs.
Template parameters
T
Type of the elements. Each element in a set container is also uniquely identified by this value (each value is itself also the element's key).
Aliased as member types set::key_type and set::value_type.Compare
A binary predicate(断言) that takes two arguments of the same type as the elements and returns a bool. The expression comp(a,b), where comp is an object of this type and a and b are key values,
shall return true if a is considered to go before b in the strict weak ordering the function defines.The set object uses this expression to determine both the order the elements follow in the container and whether two element keys are equivalent (by comparing them reflexively: they are equivalent if !comp(a,b) && !comp(b,a)). No two elements in a set container can be equivalent.
This can be a function pointer or a function object (see constructor for an example). This defaults to less, which returns the same as applying the less-than operator (a<b).
Aliased as member types set::key_compare and set::value_compare.Alloc
Type of the allocator object used to define the storage allocation model. By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.
Aliased as member type set::allocator_type.
Member types
| member type | definition | notes |
|---|---|---|
| key_type | The first template parameter (T) | |
| value_type | The first template parameter (T) | |
| key_compare | The second template parameter (Compare) | defaults to: less<key_type> |
| value_compare | The second template parameter (Compare) | defaults to: less<value_type> |
| allocator_type | The third template parameter (Alloc) | defaults to: allocator<value_type> |
| reference | allocator_type::reference | for the default allocator: value_type& |
| const_reference | allocator_type::const_reference | for the default allocator: const value_type& |
| pointer | allocator_type::pointer | for the default allocator: value_type* |
| const_pointer | allocator_type::const_pointer | for the default allocator: const value_type* |
| iterator | a bidirectional iterator to value_type | convertible to const_iterator |
| const_iterator | a bidirectional iterator to const value_type | |
| reverse_iterator | reverse_iterator | |
| const_reverse_iterator | reverse_iterator<const_iterator> | |
| difference_type | a signed integral type, identical to: iterator_traits::difference_type | usually the same as ptrdiff_t |
| size_type | an unsigned integral type that can represent any non-negative value of difference_type | usually the same as size_t |
Member functions
- (constructor) Construct set (public member function )
- (destructor) Set destructor (public member function )
- operator= Copy container content (public member function )
Iterators:
- begin Return iterator to beginning (public member function )
- end Return iterator to end (public member function )
- rbegin Return reverse iterator to reverse beginning (public member function )
- rend Return reverse iterator to reverse end (public member function )
- cbegin Return const_iterator to beginning (public member function )
- cend Return const_iterator to end (public member function )
- crbegin Return const_reverse_iterator to reverse beginning (public member function )
- crend Return const_reverse_iterator to reverse end (public member function )
Capacity:
- empty Test whether container is empty (public member function )
- size Return container size (public member function )
- max_size Return maximum size (public member function )
Modifiers:
- insert Insert element (public member function )
- erase Erase elements (public member function )
- swap Swap content (public member function )
- clear Clear content (public member function )
- emplace Construct and insert element (public member function )
- emplace_hint Construct and insert element with hint (public member function )
Observers:
- key_comp Return comparison object (public member function )
- value_comp Return comparison object (public member function )
Operations:
- find Get iterator to element (public member function )
- count Count elements with a specific value (public member function )
- lower_bound Return iterator to lower bound (public member function )
- upper_bound Return iterator to upper bound (public member function )
- equal_range Get range of equal elements (public member function )
Allocator:
- get_allocator Get allocator (public member function )
Code Example
#include <iostream>
#include <set>
using namespace std;
bool fncomp(int lhs, int rhs)
{ return lhs < rhs; }
struct classcomp
{
bool operator() (const int& lhs, const int& rhs) const
{ return lhs<rhs; }
};
int main(int argc, char **argv)
{
set<int> first;
int myints[] = {10,20,30,40,50};
set<int> first1(myints, myints+5);
set<int> first2(first1);
set<int> first3(first2.begin(),first2.end());
set<int, classcomp> first4;
bool (*fn_pt)(int,int) = fncomp;
set<int, bool(*)(int,int)> first5(fn_pt);
/** other function please to reference other container */
set<int> second;
for(int i=0; i < 10; i++)
{
second.insert(i);
}
cout << '\n';
set<int>::key_compare comp = second.key_comp();
int nNum = *(second.rbegin());
auto it = second.begin();
do{
cout << *it << '\t';
}while( comp(*(++it), nNum) );
/** outpur:0 1 2 3 4 5 6 7 8 */
set<int> third;
for(int i=0; i < 5; i++)
{
third.insert(i);
}
set<int>::value_compare valComp = third.value_comp();
nNum = *(third.rbegin());
it = third.begin();
cout << '\n';
do
{
cout << *it << '\t';
}while( valComp( *(++it), nNum ) );
return 0;
}
Reference
C++ std::set的更多相关文章
- 【NX二次开发】NX内部函数,libuifw.dll文件中的内部函数
本文分为两部分:"带参数的函数"和 "带修饰的函数". 浏览这篇博客前请先阅读: [NX二次开发]NX内部函数,查找内部函数的方法 带参数的函数: void U ...
- C++ std::priority_queue
std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...
- C++ std::queue
std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...
- C++ std::multimap
std::multimap template < class Key, // multimap::key_type class T, // multimap::mapped_type class ...
- C++ std::map
std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...
- C++ std::list
std::list template < class T, class Alloc = allocator > class list; List Lists are sequence co ...
- C++ std::forward_list
std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...
- C++ std::deque
std::deque template < class T, class Alloc = allocator > class deque; Double ended queue deque ...
- C++ std::array
std::array template < class T, size_t N > class array; Code Example #include <iostream> ...
随机推荐
- 总结:Mac前端开发环境的搭建(配置)
新年新气象,在2016年的第一天,我入手了人生中第一台自己的电脑(大一时好友赠送的电脑在一次无意中烧坏了主板,此后便不断借用别人的或者网站的).macbook air,身上已无分文...接下来半年的房 ...
- VM(虚拟机安装win7 提示 :units specified don't exist, SHSUCDX can't install)解决方法
改成IDE的模式
- Centos 下 mysql root 密码重置
重置mysql密码的方法有很多,官网也提供了很方便的快捷操作办法,可参考资料 resetting permissions .本文重置密码的具体步骤如下: 一.停止MySQL(如果处于运行状态) #se ...
- 《Django By Example》第一章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:本人目前在杭州某家互联网公司工作, ...
- Android 6.0 权限知识学习笔记
最近在项目上因为6.0运行时权限吃了亏,发现之前对运行时权限的理解不足,决定回炉重造,重新学习一下Android Permission. 进入正题: Android权限 在Android系统中,权限分 ...
- 构建通用的 React 和 Node 应用
这是一篇非常优秀的 React 教程,这篇文章对 React 组件.React Router 以及 Node 做了很好的梳理.我是 9 月份读的该文章,当时跟着教程做了一遍,收获很大.但是由于时间原因 ...
- 我的屌丝giser成长记-工作篇之B公司
从A公司跳槽到B公司,岗位还是webgis开发方向,但是具体实现的技术完全变了,从flex转换js,这也是我要离开A公司的最重要的原意之一:A公司的arcgis for flex框架采用了flexvi ...
- 使用git进行源代码管理
git是一款非常流行的分布式版本控制系统,使用Local Repository追踪代码的修改,通过Push和Pull操作,将代码changes提交到Remote Repository,或从Remote ...
- 解决WINDOWS防火墙开启后Ping不通
WINDOWS系统由于安全考虑,当开启防火墙时,默认不允许外主机对其进行ping功能,即别的电脑ping不通本机.别的主机ping不通本机是因为本机的防火墙关闭了ICMP回显功能,只要把这回显功能打开 ...
- 在Mac OS X上安装ASP.NET 5(译文)
ASP.NET 5 运行在包括OS X的可用于多个平台的.NET Execution Environment(DNX)上.本文介绍如何在OS X上通过HomeBrew安装DNX和ASP.NET 5. ...