set集合用于存放不重复的元素

template <class Key, class Compare = less<Key>, class Alloc = alloc>
class set
{
......
private:
typedef rb_tree<key_type identity="" ,=""><value_type value_type,="">, key_compare, Alloc> rep_type;
rep_type t; //set内部的实现实际上是一个红黑树
......
};

Constructors

set

Constructs a set that is empty or that is a copy of all or part of some other set.

set默认的比较器是不允许 有相同元素的,不过你可以重新定义一个比较器

Typedefs

allocator_type

A type that represents the allocator class for the set object.

const_iterator

A type that provides a bidirectional iterator that can read a const element in the set.

const_pointer

A type that provides a pointer to a const element in a set.

const_reference

A type that provides a reference to a const element stored in a set for reading and performing const operations.

const_reverse_iterator

A type that provides a bidirectional iterator that can read any const element in the set.

difference_type

A signed integer type that can be used to represent the number of elements of a set in a range between elements pointed to by iterators.

iterator

A type that provides a bidirectional iterator that can read or modify any element in a set.

key_compare

A type that provides a function object that can compare two sort keys to determine the relative order of two elements in the set.

key_type

The type describes an object stored as an element of a set in its capacity as sort key.

pointer

A type that provides a pointer to an element in a set.

reference

A type that provides a reference to an element stored in a set.

reverse_iterator

A type that provides a bidirectional iterator that can read or modify an element in a reversed set.

size_type

An unsigned integer type that can represent the number of elements in a set.

value_compare

The type that provides a function object that can compare two elements to determine their relative order in the set.

value_type

The type describes an object stored as an element of a set in its capacity as a value.

Member Functions

begin

Returns an iterator that addresses the first element in the set.

clear

Erases all the elements of a set.

如果当前容器中的元素是自己new出来的对象指针,那么在调用clear或erase函数之前,需要自己先释放掉这部分内存。

否则会有内存泄露。

count

Returns the number of elements in a set whose key matches a parameter-specified key.

set<int> s1; set<int>::size_type i;

s1.insert(1); s1.insert(1); // Keys must be unique in set, so duplicates are ignored i = s1.count(1);

cout << "The number of elements in s1 with a sort key of 1 is: " << i << "." << endl;

i = s1.count(2);

cout << "The number of elements in s1 with a sort key of 2 is: " << i << "." << endl;

结果分别为1和0.

对于set而言,只存在1(表示关键字在set中)或0(关键字不在set中)两种情况。

empty

Tests if a set is empty.

end

Returns an iterator that addresses the location succeeding the last element in a set.

equal_range

Returns a pair of iterators respectively to the first element in a set with a key that is greater than a specified key and to the first element in the set with a key that is equal to or greater than the key.

pair<iterator,iterator> equal_range(const key_type & x) const
返回容器中关键字等于x(满足==x关系)的元素区间。返回值[pair.first, pair.second]均满足该关系。

如果只是判断元素是否存在,可以使用count函数检查返回值;

erase

Removes an element or a range of elements in a set from specified positions or removes elements that match a specified key.

如果当前容器中的元素是自己new出来的对象指针,那么在调用clear或erase函数之前,需要自己先释放掉这部分内存。

否则会有内存泄露。

find

Returns an iterator addressing the location of an element in a set that has a key equivalent to a specified key.

get_allocator

Returns a copy of the allocator object used to construct the set.

insert

Inserts an element or a range of elements into a set.

pair<iterator, bool> insert(const value_type& x)
将一个元素插入当前set容器。返回值pair的第一个元素,表示插入位置的迭代器;第二个元素,表示是否插入到容器中,只有该值为true,迭代器才有效。

检查是否插入成功可以如下:

std::pair<std::set<int>::iterator,bool> insert_pair;
insert_pair = set_nums.insert(RandomOneNum(min,max,bNegative));
if (insert_pair.second == true)
{
pNums[nIndex++] = *insert_pair.first;
}

key_comp

Retrieves a copy of the comparison object used to order keys in a set.

lower_bound

Returns an iterator to the first element in a set with a key that is equal to or greater than a specified key.

max_size

Returns the maximum length of the set.

rbegin

Returns an iterator addressing the first element in a reversed set.

rend

Returns an iterator that addresses the location succeeding the last element in a reversed set.

size

Returns the number of elements in the set.

swap

Exchanges the elements of two sets.

upper_bound

Returns an iterator to the first element in a set with a key that is greater than a specified key.

value_comp

Retrieves a copy of the comparison object used to order element values in a set.

operator!= (set)

Tests if the set or multiset object on the left side of the operator is not equal to the set or multiset object on the right side.

operator< (set)

Tests if the set or multiset object on the left side of the operator is less than the set or multiset object on the right side.

operator<= (set)

Tests if the set or multiset object on the left side of the operator is less than or equal to the set or multiset object on the right side.

operator== (set)

Tests if the set or multiset object on the left side of the operator is equal to the set or multiset object on the right side.

operator> (set)

Tests if the set or multiset object on the left side of the operator is greater than the set or multiset object on the right side.

operator>= (set)

Tests if the set or multiset object on the left side of the operator is greater than or equal to the set or multiset object on the right side.

set也重载了[]运算符,可以像数组一样使用

注意的是set中数元素的值不能直接被改变

template <class Key, class Compare, class Alloc>
inline bool operator == (const set<Key, Compare, Alloc> & x, const set<Key, Compare, Alloc> & y)
判断两个set容器是否相等。全局函数,非成员函数。

template <class Key, class Compare, class Alloc>
inline bool operator < (const set<Key, Compare, Alloc> & x, const set<Key, Compare, Alloc< & y)
判断容器x是否小于y。全局函数,非成员函数。

set用法总结的更多相关文章

  1. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  2. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  3. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  4. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  5. python enumerate 用法

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

  6. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

  7. 【JavaScript】innerHTML、innerText和outerHTML的用法区别

    用法: <div id="test">   <span style="color:red">test1</span> tes ...

  8. chattr用法

    [root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...

  9. 萌新笔记——vim命令“=”、“d”、“y”的用法(结合光标移动命令,一些场合会非常方便)

    vim有许多命令,网上搜有一堆贴子.文章列举出各种功能的命令. 对于"="."d"."y",我在无意中发现了它们所具有的相同的一些用法,先举 ...

  10. [转]thinkphp 模板显示display和assign的用法

    thinkphp 模板显示display和assign的用法 $this->assign('name',$value); //在 Action 类里面使用 assign 方法对模板变量赋值,无论 ...

随机推荐

  1. Vi命令详解

    Vi有三种模式,分别为命令行模式.一般模式和编辑模式.在命令行输入“Vi 文件名”,即可进入Vi.常用命令如下:一.一般模式翻页[Ctrl]+[f]: 向下翻一页,相当于[Page Down]按键.[ ...

  2. js实现网页收藏功能,动态添加删除网址

    <html> <head> <title> 动态添加删除网址 </title> <meta charset="utf-8"&g ...

  3. 响应式Asp.net MVC企业网站源码

    最近时间充裕,自己写了一个响应式MVC企业网站系统,用于回顾自己的MVC知识.网站源码后台和前台都采用响应式布局,可以适应不同的屏幕. 一.源码描述 响应式企业网站系统,前台和后台都采用了响应式布局, ...

  4. GCD 单例

    + (ThemeManager *)sharedInstance { static   dispatch_once_t   once; //只执行一次 static   ThemeManager   ...

  5. cf D. Levko and Array

    http://codeforces.com/contest/361/problem/D 用二分搜索相邻两个数的差的绝对值,然后用dp记录数改变的次数.dp[i]表示在i之前改变的次数,如果|a[i]- ...

  6. RSYSLOG没那么简单

    定义系统默认的日志收集还算EASY. 但如何在公司项目里要配置程序员们写的自定义日志,那可能就要用到LOCAL及FILTER过滤这些东东了... 慢慢走吧.. 收集URL备用,都是讲LOCAL,TEM ...

  7. Codeforces 219D Choosing Capital for Treeland

    http://codeforces.com/problemset/problem/219/D 题目大意: 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达 ...

  8. How can I let the compiled script depend on something dynamic

    Compile your script with /DNAME=value or /X"nsis command" passed on to makensis.exe as com ...

  9. Linux环境下使用JFS文件系统

    Linux环境下使用JFS文件系统 JFS是IBM公司为linux系统开发的一个日志文件系统.从IBM的实力及它对Linux的态度来看,JFS应该是未来日志文件系统中最具实力的一个文件系统. JFS提 ...

  10. jquery 的ready() 与window.onload()的区别

    做web开发时常用Jquery中$(document).ready()和JavaScript中的window.onload方法,两者都是要在页面加载完成以后加载的方法,但是这两者还是有很大区别的.最近 ...