书法字典:https://www.shufadict.com

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

posted on   翰墨小生  阅读(12233)  评论(2编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述

导航

< 2010年6月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 1 2 3
4 5 6 7 8 9 10
书法字典:https://www.shufadict.com
点击右上角即可分享
微信分享提示