STL-set

简介:

简介

set是一种随机存储的关联式容器,其关键词(key)和元素(value)是同一个值。set之中所有元素互不相同。set是通过二叉查找树来实现的。

创建

创建一个空的set

  1: set<int> s0 ;

创建一个带大于比较器的set, 默认是小于比较器less<int>

  1: set<int, greater<int>> s1 ;

用数组初始化一个set

  1: int a[3] = {1, 2, 3} ;
  2: set<int> s2(a, a + 3) ;

用拷贝构造函数初始化set

  1: set<int> s1 ;
  2: set<int> s2(s1) ;

区间初始化

  1: set<int> s1 ;
  2: set<int> s2(s1.begin(), s1.end()) ;

自定义比较函数

以类为比较器

  1: struct classcmp
  2: {
  3:   bool operator()(const int& lhs, const int& rhs)
  4:   {
  5:     return lhs < rhs ;
  6:   }
  7: };
  8: 
  9: int main(void)
 10: {
 11:   set<int, classcmp> s5 ;
 12: 
 13:   system("pause") ;
 14:   return 0 ;
 15: }

以函数指针为比较器

  1: bool fncmp(int lhs, int rhs)
  2: {
  3:   return lhs < rhs ;
  4: }
  5: 
  6: int main(void)
  7: {
  8:   bool(*fn_pt)(int, int) = fncmp ;
  9:   set<int, bool(*)(int, int)> s1(fn_pt) ;
 10: 
 11:   system("pause") ;
 12:   return 0 ;
 13: }

 

遍历

正向遍历

使用while

  1: int a[3] = {1, 2, 3} ;
  2: set<int> s(a, a + 3) ;
  3: 
  4: set<int>::const_iterator itor ;
  5: itor = s.begin() ;
  6: 
  7: while (itor != s.end())
  8: {
  9:   cout << *itor << endl ;
 10:   ++itor ;
 11: }

使用for

  1: int a[3] = {1, 2, 3} ;
  2: set<int> s(a, a + 3) ;
  3: 
  4: set<int>::const_iterator itor ;
  5: for (itor = s.begin(); itor != s.end(); ++itor)
  6: {
  7:   cout << *itor << endl ;
  8: }

 

反向遍历

使用while

  1: int a[3] = {1, 2, 3} ;
  2: set<int> s(a, a + 3) ;
  3: 
  4: set<int>::const_reverse_iterator ritor ;
  5: ritor = s.rbegin() ;
  6: 
  7: while (ritor != s.rend())
  8: {
  9:   cout << *ritor << endl ;
 10:   ++ritor ;
 11: }

使用for

  1: int a[3] = {1, 2, 3} ;
  2: set<int> s(a, a + 3) ;
  3: 
  4: set<int>::const_reverse_iterator ritor ;
  5: for (ritor = s.rbegin(); ritor != s.rend(); ++ritor)
  6: {
  7:   cout << *ritor << endl ;
  8: }

插入

插入单个值

  1: set<int> s ;
  2: s.insert(1) ;

批量插入

插入整个数组

  1: int a[3] = {1, 2, 3} ;
  2: set<int> s ;
  3: s.insert(a, a + 3) ;

插入其他set的值

  1: int a[3] = {1, 2, 3} ;
  2: set<int> s(a, a + 3) ;
  3: 
  4: set<int> s1 ;
  5: s1.insert(s.begin(), s.end()) ;

删除

  1: set<int> s ;
  2: for (int i = 1; i <= 5; ++i)
  3:   s.insert(i) ;
  4: 
  5: set<int>::const_iterator citor ;
  6: citor = s.begin() ;
  7: ++citor ; // citor now point to 2
  8: 
  9: // 删除单个元素
 10: s.erase(citor) ; // erase 2 ; 
 11: 
 12: //批量删除
 13: citor = s.find(3) ; // itor now point to 3
 14: s.erase(citor, s.end()) ; // erase 3, 4, 5
 15: 
 16: //删除所有元素
 17: s.erase(s.begin(), s.end()) ;// erase all elements, same as s.clear()

查找

find

  1: set<int> s ;
  2: for (int i = 1; i <= 5; ++i)
  3:   s.insert(i) ;
  4: 
  5: set<int>::iterator itor ;
  6: itor = s.find(4) ;
  7: if(itor != s.end()) // itor point to s.end() if not found
  8:   cout << "found" ;
  9: else
 10:   cout << "not found" ;

count

  1: set<int> s ;
  2: for (int i = 1; i <= 5; ++i)
  3:   s.insert(i) ;
  4: 
  5: set<int>::iterator itor ;
  6: if(s.count(4) == 1) // return 1 if s contains 4, else 0
  7:   cout << "s contains 4" ;
  8: else
  9:   cout << "s does not contains 4" ;

排序

由于set本身是有序的,所以不提供排序函数。

本文转自zdd博客园博客,原文链接:http://www.cnblogs.com/graphics/archive/2010/06/01/1749569.html,如需转载请自行联系原作者

相关文章
|
5月前
|
存储 C++ 容器
C++:STL - set & map
C++:STL - set & map
38 4
|
5月前
|
存储 C++ 容器
【STL】map和set的原理及其使用
【STL】map和set的原理及其使用
|
5月前
|
编译器 容器
简易实现 STL--list
简易实现 STL--list
|
C++ 索引 容器
【C++ STL】 --- map
【C++ STL】 --- map
72 0
|
5月前
|
C++
stl中set、map的用法
stl中set、map的用法
|
10月前
|
存储 自然语言处理 C++
C++ STL中 set和map介绍以及使用方法
C++ STL中 set和map介绍以及使用方法
116 1
|
自然语言处理 容器
C++STL——map与set介绍及使用
C++STL——map与set介绍及使用
|
存储 自然语言处理 C++
【C++进阶】四、STL---set和map的介绍和使用
目录 一、关联式容器 二、键值对 三、树形结构的关联式容器 四、set的介绍及使用 4.1 set的介绍 4.2 set的使用 五、multiset的介绍及使用 六、map的介绍和使用 6.1 map的介绍 6.2 map的使用 七、multimap的介绍和使用
72 0
【C++进阶】四、STL---set和map的介绍和使用
|
存储 数据处理 C++
STL之set,map
STL之set,map
|
存储 自然语言处理 C++
【STL】set、map的使用介绍
用来表示具有一一对应关系的一种数据结构,该结构中只包含两个成员变量key和value,key代表关键字,value表示关键字对应的值。比如:现在要建立一个英译汉的词典,那该词典中必然有英文单词和与其对应的中文含义,而且,英文单词与中文含义是一一对应的关系,