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本身是有序的,所以不提供排序函数。

你可能感兴趣的:(set)