C++ set容器-56-集合容器大小/交换/插入/删除操作

悠悠 2022-11-26 07:41 287阅读 0赞

这篇学习set容器的大小操作,大小主要有判断是否为空,size()还有交换swap(),最后来学习下set容器的插入和删除操作。基本上那些常用的API,前面都学习过。

1.set容器大小操作和交换

函数原型

2020081022354381.png

注意这里set容器没有resize(),重新指定容器大小的操作。

  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4. using namespace std;
  5. void printSet(const set<int>& st)
  6. {
  7. for(set<int>::const_iterator it = st.begin(); it != st.end(); it++)
  8. {
  9. cout << *it << " ";
  10. }
  11. cout << endl;
  12. }
  13. void test01()
  14. {
  15. // set的大小操作
  16. set<int> st;
  17. // 插入元素
  18. st.insert(20);
  19. st.insert(30);
  20. st.insert(10);
  21. st.insert(50);
  22. st.insert(50);
  23. printSet(st);
  24. // 判断是否为空
  25. cout << "is Empty?" << st.empty() << endl;
  26. // 返回set容器的大小
  27. cout << "size: " << st.size() << endl;
  28. // 容器交换
  29. set<int> st1;
  30. // 插入元素
  31. st1.insert(200);
  32. st1.insert(300);
  33. cout << "before swap " << endl;
  34. printSet(st);
  35. printSet(st1);
  36. cout << "after swap " << endl;
  37. st1.swap(st);
  38. printSet(st);
  39. printSet(st1);
  40. }
  41. int main()
  42. {
  43. test01();
  44. system("pause");
  45. return 0;
  46. }

运行结果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE1NDE5NDY_size_16_color_FFFFFF_t_70

2.set容器的插入操作

set容器插入比较特殊,没有前面提供哪些push_back,只有一个insert(ele),而且不能指定插入位置,参数就是一个元素,由于set容器会自动排序,所以插入的api就一个insert(ele),这个插入的方法,上面代码也使用到了,就不再练习。

直接来看删除相关

函数原型

20200810224600675.png

接下来用代码来练习一下几个删除的方法

  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4. using namespace std;
  5. void printSet(const set<int>& st)
  6. {
  7. for(set<int>::const_iterator it = st.begin(); it != st.end(); it++)
  8. {
  9. cout << *it << " ";
  10. }
  11. cout << endl;
  12. }
  13. void test01()
  14. {
  15. // set的大小操作
  16. set<int> st;
  17. // 插入元素
  18. st.insert(20);
  19. st.insert(30);
  20. st.insert(10);
  21. st.insert(50);
  22. printSet(st);
  23. // 根据迭代器位置删除元素
  24. st.erase(st.begin());
  25. printSet(st);
  26. // 根据元素删除
  27. st.erase(50);
  28. printSet(st);
  29. // 清空容器
  30. st.clear();
  31. cout << "is Empty? " << st.empty() << endl;
  32. }
  33. int main()
  34. {
  35. test01();
  36. system("pause");
  37. return 0;
  38. }

运行结果

20200810225026865.png

3.set容器的查找和统计

查找元素是否存在和统计元素的个数

函数原型

20200811224433497.png

注意,find()函数的返回值是一个迭代器,也就是调用这个函数,是通过迭代器一个一个去帮你查找,如果查找到就返回迭代器的位置,如果找不到就返回迭代器最后查找的位置,也就是迭代器查找到最后还是没有找到元素。统计函数count()的结果无非就是0和1

测试代码

  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4. using namespace std;
  5. void printSet(const set<int>& st)
  6. {
  7. for(set<int>::const_iterator it = st.begin(); it != st.end(); it++)
  8. {
  9. cout << *it << " ";
  10. }
  11. cout << endl;
  12. }
  13. void test01()
  14. {
  15. // set的大小操作
  16. set<int> st;
  17. // 插入元素
  18. st.insert(20);
  19. st.insert(30);
  20. st.insert(10);
  21. st.insert(50);
  22. printSet(st);
  23. // 查找元素
  24. set<int>::iterator pos = st.find(30);
  25. if(pos != st.end())
  26. {
  27. cout << "had find " << endl;
  28. }
  29. else
  30. {
  31. cout << "not find " << endl;
  32. }
  33. // 统计
  34. int cot = st.count(30);
  35. cout << "the count of element 30 is " << cot << endl;
  36. }
  37. int main()
  38. {
  39. test01();
  40. system("pause");
  41. return 0;
  42. }

运行结果

20200811225400987.png

发表评论

表情:
评论列表 (有 0 条评论,287人围观)

还没有评论,来说两句吧...

相关阅读