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> ...
随机推荐
- 火焰图分析openresty性能瓶颈
注:本文操作基于CentOS 系统 准备工作 用wget从https://sourceware.org/systemtap/ftp/releases/下载最新版的systemtap.tar.gz压缩包 ...
- 【.net 深呼吸】启动一个进程并实时获取状态信息
地球人和火星人都知道,Process类既可以获取正在运行的进程,也可以启动一个新的进程.在79.77%应用场合,我们只需要让目标进程顺利启动就完事了,至于它执行了啥,有没有出错,啥时候退出就不管了. ...
- 【HanLP】资料链接汇总
Java中调用HanLP配置 HanLP自然语言处理包开源官方文档 了解HanLP的全部 自然语言处理HanLP 开源自由的汉语言处理包主页 GitHub源码 基于hanLP的中文分词详解-MapRe ...
- 普通程序员如何转向AI方向
眼下,人工智能已经成为越来越火的一个方向.普通程序员,如何转向人工智能方向,是知乎上的一个问题.本文是我对此问题的一个回答的归档版.相比原回答有所内容增加. 一. 目的 本文的目的是给出一个简单的,平 ...
- 基于注解的bean配置
基于注解的bean配置,主要是进行applicationContext.xml配置.DAO层类注解.Service层类注解. 1.在applicationContext.xml文件中配置信息如下 &l ...
- isEmpty和isNull()区别
isEmpty和isNull()区别一个NULL字符串一定是一个空串,一个空串未必是一个NULL字符串例如:QString().isNull(): //结果为trueQString().isEm ...
- MediatorPattern(中介者模式)
/** * 中介者模式 * @author TMAC-J * 研究了这么多设计模式,觉得无非就是几点: * 1.若两个类有耦合关系,设立一个中间类,处理两个类的关系,把两个类的耦合降低 * 2.面向接 ...
- canvas快速绘制圆形、三角形、矩形、多边形
想看前面整理的canvas常用API的同学可以点下面: canvas学习之API整理笔记(一) canvas学习之API整理笔记(二) 本系列文章涉及的所有代码都将上传至:项目代码github地址,喜 ...
- BPM配置故事之案例1-配置简单流程
某天,Boss找到了信息部工程师小明. Boss:咱们新上了H3 BPM,你研究研究把现在的采购申请流程加上去吧,这是采购申请单. 小明:好嘞 采购申请单 小明回去后拿着表单想了想,开始着手配置. 他 ...
- 从史上八大MySQL事故中学到的经验
本文列举了史上八大MySQL宕机事件原因.影响以及人们从中学到的经验,文中用地震级数来类比宕机事件的严重性和后果,排在最严重层级前两位的是由于亚马逊AWS宕机故障(相当于地震十级和九级). 一.Per ...