STL 标准模板库之<set>详解

        浏览本文之前,事先告知,本文由两个部分,如果你只是忘记了<set>的某些常用的用法,那只要看前半部分《STL_<set>快速养成》,如果你是初次接触set并且想要做一番深究的话,可以把整篇都看完,相信大家或多或少都会有所收获微笑


一。《STL_<set>快速养成》


   1.<set>的特性

       <set>中所有的元素都互不相同,并且是有序的(默认从小到大),在内部是通过二叉查找树实现,与map不同的是其关键词(key)和值(value)相等

   2.如何创建

     -创建一个空的set

set<int> set_empty
     -创建一个带大于比较器的set,默认是小于比较器less<int>
set<int,greater<int>> set_greater
     -用数组初始化一个set
int array[3]={1,2,3};
set<int> set_array(array,array+3);
     -用拷贝构造函数初始化set
set<int> set_1;
set<int> set_2(set_1);
     -区间初始化set
set<int> set_1;
set<int> set_2(set_1.begin(),set_1.end())
     -自定义比较器

        以类为比较器

  struct classcmp
   {
     bool operator()(const int& lhs, const int& rhs)
     {
       return lhs < rhs ;
     }
   };
   
  int main(void)
  {
    set<int, classcmp> s5
    return 0;
  }
       以函数指针为比较器
bool fncmp(int lhs, int rhs)
   {
     return lhs < rhs ;
   }
   
   int main(void)
   {
     bool(*fn_pt)(int, int) = fncmp ;
     set<int, bool(*)(int, int)> s1(fn_pt) ;
  
    system("pause") ;
    return 0 ;
  }
   3.如何遍历

     -正向遍历

          使用while()

 int a[3] = {1, 2, 3} ;
   set<int> s(a, a + 3) ;
   
   set<int>::const_iterator itor ;
   itor = s.begin() ;
   
   while (itor != s.end())
   {
     cout << *itor << endl ;
    ++itor ;
  }
          使用for()
 int a[3] = {1, 2, 3} ;
   set<int> s(a, a + 3) ;
   
   set<int>::const_iterator itor ;
   for (itor = s.begin(); itor != s.end(); ++itor)
   {
     cout << *itor << endl ;
   }
     -反向遍历
          使用while()
 int a[3] = {1, 2, 3} ;
   set<int> s(a, a + 3) ;
   
   set<int>::const_reverse_iterator ritor ;
   ritor = s.rbegin() ;
   
   while (ritor != s.rend())
   {
     cout << *ritor << endl ;
    ++ritor ;
  }
          使用for()
 int a[3] = {1, 2, 3} ;
   set<int> s(a, a + 3) ;
   
   set<int>::const_reverse_iterator ritor ;
   for (ritor = s.rbegin(); ritor != s.rend(); ++ritor)
   {
    cout << *ritor << endl ;
   }
   4.如何插入
     -插入单个值
set<int> s;
s.insert(value);

     -插入整个数组

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

     -插入其他set的值
 int a[3] = {1, 2, 3} ;
 set<int> s(a, a + 3) ;
 set<int> s1 ;
 s1.insert(s.begin(), s.end()) ;
   5.如何删除
 set<int> s ;
  for (int i = 1; i <= 5; ++i)
     s.insert(i) ;
   
   set<int>::const_iterator citor ;
   citor = s.begin() ;
   ++citor ; // citor now point to 2
   
   // 删除单个元素
  s.erase(citor) ; // erase 2 ; 
  
  //批量删除
  citor = s.find(3) ; // itor now point to 3
  s.erase(citor, s.end()) ; // erase 3, 4, 5
  
  //删除所有元素
  s.erase(s.begin(), s.end()) ;// erase all elements, same as s.clear()
   6.如何查找

     -find()

  set<int> s ;
   for (int i = 1; i <= 5; ++i)
     s.insert(i) ;
   
   set<int>::iterator itor ;
   itor = s.find(4) ;
   if(itor != s.end()) // itor point to s.end() if not found
     cout << "found" ;
   else
    cout << "not found" ;
     -count()
  set<int> s ;
   for (int i = 1; i <= 5; ++i)
     s.insert(i) ;
   
   set<int>::iterator itor ;
   if(s.count(4) == 1) // return 1 if s contains 4, else 0
     cout << "s contains 4" ;
   else
     cout << "s does not contains 4" ;

OK,速成到这里就结束了,想必大家已经可以照葫芦画瓢写出代码了,接下来是对其内部的解释,当然不是我写的,是一篇类似于API的文章


C++  STL之Set容器的用法

1   set中的元素类型... 1

2     set中构造相关函数... 2

3     set中的迭代器... 3

4     set中的容量相关函数... 3

5     set中元素修改函数... 3

5.1      insert 函数... 3

5.2      erase 函数... 4

5.3      clear 函数... 5

5.4      swap 函数... 5

5.5      emplace 函数... 5

5.6      emplace_hint函数... 5

6     set 中的比较函数体... 6

6.1      key_com 函数... 6

6.2      value_com函数... 6

7     set的其他操作函数... 7

7.1      find 函数... 7

7.2      count 函数... 8

7.3  lower_bound 函数... 8

7.4      upper_bound函数... 8

7.5  equal_range 函数... 9

7.6  get_allocator 函数... 10

Set容器是一个关联容器,容器中的元素互不相同,并且容器中的元素按照键值大小进行排序。每当插入或者删除一个元素,容器都会重新排序。Set容器有两大特点,一个是元素排序,另一个就是查询速度快(当然没有vector快)。Set获取元素时通过键值,关联容器都这样。Set是通过二元查找树实现的,再具体点就是红黑树。

set中的元素类型

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: 

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值