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. 使用ServiceStack构建Web服务

    提到构建WebService服务,大家肯定第一个想到的是使用WCF,因为简单快捷嘛.首先要说明的是,本人对WCF不太了解,但是想快速建立一个WebService,于是看到了MSDN上的这一篇文章 Bu ...

  2. 渗透测试工具BurpSuite做网站的安全测试(基础版)

    渗透测试工具BurpSuite做网站的安全测试(基础版) 版权声明:本文为博主原创文章,未经博主允许不得转载. 学习网址: https://t0data.gitbooks.io/burpsuite/c ...

  3. Android GridView 通过seletor 设置状态和默认状态

    Android中可以通过selector控制GridView Item 的状态,而省去使用代码控制 GridView View Selector Xml文件 <?xml version=&quo ...

  4. 23种设计模式--工厂模式-Factory Pattern

    一.工厂模式的介绍       工厂模式让我们相到的就是工厂,那么生活中的工厂是生产产品的,在代码中的工厂是生产实例的,在直白一点就是生产实例的类,代码中我们常用new关键字,那么这个new出来的实例 ...

  5. windows环境下sublime的nodejs插件详细安装图解

    前面的话   搜索了好多文档后,才成功地安装了sublime text3的nodejs插件.为了存档,也为了方便有同样需求的朋友,将其安装过程详细记录如下 安装nodejs 虽然nodejs官网提供了 ...

  6. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

  7. if __name__== "__main__" 的意思(作用)python代码复用

    if __name__== "__main__" 的意思(作用)python代码复用 转自:大步's Blog  http://www.dabu.info/if-__-name__ ...

  8. 从国内流程管理软件市场份额看中国BPM行业发展

    随着互联网+.中国制造2025.工业4.0等国家战略的支持与引导,企业在数字经济时代的信息化表现惊人,越来越多企业认识到,对于企业的发展来说,信息自动化远远还不够,企业的战略.业务和IT之间需保持高度 ...

  9. 【C#】获取网页内容及HTML解析器HtmlAgilityPack的使用

    最近经常需要下载一些东西,而这个下载地址又会经过层层跳转,每个页面上都有很多广告,烦不胜烦,所以做了一个一键获得最终下载地址的小工具.使用C#,来获取网页内容,然后通过HtmlAgilityPack获 ...

  10. 程序猿是如何解决SQLServer占CPU100%的

    文章目录 遇到的问题 使用SQLServer Profiler监控数据库 SQL1:查找最新的30条告警事件 SQL2:获取当前的总报警记录数 有哪些SQL语句会导致CPU过高? 查看SQL的查询计划 ...