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. CoreCRM 开发实录 —— Profile

    再简单的功能,也需要一坨代码的支持.Profile 的编辑功能主要就是修改个人的信息.比如用户名.头像.性别.电话--虽然只是一个编辑界面,但添加下来,涉及了6个文件的修改和7个新创建的文件.各种生成 ...

  2. 戏说HTML5

    如果有非技术人员问你,HTML5是什么,你会怎么回答? 新的HTML规范... 给浏览器提供了牛逼能力,干以前不能干的事...(确切地说应该是给浏览器规定了许多新的接口标准,要求浏览器实现牛逼的功能. ...

  3. Linux scp 设置nohup后台运行

    Linux scp 设置nohup后台运行 1.正常执行scp命令 2.输入ctrl + z 暂停任务 3.bg将其放入后台 4.disown -h 将这个作业忽略HUP信号 5.测试会话中断,任务继 ...

  4. JavaScript权威指南 - 对象

    JavaScript对象可以看作是属性的无序集合,每个属性就是一个键值对,可增可删. JavaScript中的所有事物都是对象:字符串.数字.数组.日期,等等. JavaScript对象除了可以保持自 ...

  5. 展望未来:使用 PostCSS 和 cssnext 书写 CSS

    原文链接:A look into writing future CSS with PostCSS and cssnext 译者:nzbin 像twitter,google,bbc使用的一样,我打算看一 ...

  6. C#多线程之线程同步篇2

    在上一篇C#多线程之线程同步篇1中,我们主要学习了执行基本的原子操作.使用Mutex构造以及SemaphoreSlim构造,在这一篇中我们主要学习如何使用AutoResetEvent构造.Manual ...

  7. 【开源】.Net 动态脚本引擎NScript

    开源地址: https://git.oschina.net/chejiangyi/NScript 开源QQ群: .net 开源基础服务  238543768 .Net 动态脚本引擎 NScript   ...

  8. 计算机程序的思维逻辑 (54) - 剖析Collections - 设计模式

    上节我们提到,类Collections中大概有两类功能,第一类是对容器接口对象进行操作,第二类是返回一个容器接口对象,上节我们介绍了第一类,本节我们介绍第二类. 第二类方法大概可以分为两组: 接受其他 ...

  9. Linux常用指令指南,终端装逼利器

    最近搞了台Macbook Pro,就学习了一下Linux命令,在网上查了些资料,看了本书叫<快乐的 Linux 命令行>,里面涉及到了各个方面的命令. 在此将常用的整理出来,以备将来使用. ...

  10. [译]处理文本数据(scikit-learn 教程3)

    原文网址:http://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html 翻译:Tacey Won ...