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

cplusplus

C++ std::set的更多相关文章

  1. 【NX二次开发】NX内部函数,libuifw.dll文件中的内部函数

    本文分为两部分:"带参数的函数"和 "带修饰的函数". 浏览这篇博客前请先阅读: [NX二次开发]NX内部函数,查找内部函数的方法 带参数的函数: void U ...

  2. C++ std::priority_queue

    std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...

  3. C++ std::queue

    std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...

  4. C++ std::multimap

    std::multimap template < class Key, // multimap::key_type class T, // multimap::mapped_type class ...

  5. C++ std::map

    std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...

  6. C++ std::list

    std::list template < class T, class Alloc = allocator > class list; List Lists are sequence co ...

  7. C++ std::forward_list

    std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...

  8. C++ std::deque

    std::deque template < class T, class Alloc = allocator > class deque; Double ended queue deque ...

  9. C++ std::array

    std::array template < class T, size_t N > class array; Code Example #include <iostream> ...

随机推荐

  1. JS正则表达式常用总结

    正则表达式的创建 JS正则表达式的创建有两种方式: new RegExp() 和 直接字面量. //使用RegExp对象创建 var regObj = new RegExp("(^\\s+) ...

  2. C#中那些[举手之劳]的性能优化

    隔了很久没写东西了,主要是最近比较忙,更主要的是最近比较懒...... 其实这篇很早就想写了 工作和生活中经常可以看到一些程序猿,写代码的时候只关注代码的逻辑性,而不考虑运行效率 其实这对大多数程序猿 ...

  3. 【WCF】错误协定声明

    在上一篇烂文中,老周给大伙伴们介绍了 IErrorHandler 接口的使用,今天,老周补充一个错误处理的知识点——错误协定. 错误协定与IErrorHandler接口不同,大伙伴们应该记得,上回我们 ...

  4. Zabbix基本配置及监控主机

    监控主机一版需要在被监控的主机上安装Zabbix Agent 监控主机 安装zabbix-agent 首先需要在被监控的主机上安装agent,可以下载预编译好的RPM进行安装,下载地址:http:// ...

  5. Android业务组件化之现状分析与探讨

    前言: 从个人经历来说的话,从事APP开发这么多年来,所接触的APP的体积变得越来越大,业务的也变得越来越复杂,总来来说只有一句话:这是一个APP臃肿的时代!所以为了告别APP臃肿的时代,让我们进入一 ...

  6. Python应用03 使用PyQT制作视频播放器

    作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁任何形式转载. 最近研究了Python的两个GUI包,Tkinter和PyQT.这两个GUI包的底层分别是Tcl/ ...

  7. nodejs模块发布及命令行程序开发

    前置技能 npm工具为nodejs提供了一个模块和管理程序模块依赖的机制,当我们希望把模块贡献出去给他人使用时,可以把我们的程序发布到npm提供的公共仓库中,为了方便模块的管理,npm规定要使用一个叫 ...

  8. angularjs 依赖注入--自己学着实现

    在用angular依赖注入时,感觉很好用,他的出现是 为了"削减计算机程序的耦合问题" ,我怀着敬畏与好奇的心情,轻轻的走进了angular源码,看看他到底是怎么实现的,我也想写个 ...

  9. 阿里云学生优惠Windows Server 2012 R2安装IIS,ftp等组件,绑定服务器域名,域名解析到服务器,域名备案,以及安装期间错误的解决方案

     前言: 这几天终于还是按耐不住买了一个月阿里云的学生优惠.只要是学生,在学信网上注册过,并且支付宝实名认证,就可以用9块9的价格买阿里云的云服务ECS.确实是相当的优惠. 我买的是Windows S ...

  10. C语言动态走迷宫

    曾经用C语言做过的动态走迷宫程序,先分享代码如下: 代码如下: //头文件 #include<stdio.h> #include<windows.h>//Sleep(500)函 ...