• STL入门基础 map和set容器


    目录

    set容器的接口使用

    map容器的接口使用


    STL是C++标准库的重要组成部分,map和set容器在实际中非常的重要,关联式容器里面存储的是结构的键值对,这在数据检索时比序列式容器效率更高

    树型关联式容器

    树型结构的关联式容器主要有四种:map、set、multimap、multiset,他们使用红黑树作为其底层,容器中的元素是一个有序的序列

    set容器的接口使用

    set和multiset容器的插入

    1. //set插入 排序+去重
    2. void test_set1()
    3. {
    4. set<int> s;
    5. s.insert(5);
    6. s.insert(6);
    7. s.insert(2);
    8. s.insert(4);
    9. s.insert(3);
    10. s.insert(2);
    11. s.insert(5);
    12. set<int>::iterator it = s.begin();
    13. while (it != s.end())
    14. {
    15. //*it = 10;不支持修改
    16. cout << *it << " ";//打印2 3 4 5 6 出来有序+去重
    17. ++it;
    18. }
    19. cout << endl;
    20. }
    21. //multiset 排序+不去重
    22. void test_set5()
    23. {
    24. multiset<int> s;
    25. s.insert(5);
    26. s.insert(6);
    27. s.insert(2);
    28. s.insert(4);
    29. s.insert(3);
    30. s.insert(2);
    31. s.insert(5);
    32. set<int>::iterator it = s.begin();
    33. while (it != s.end())
    34. {
    35. //*it = 10;不支持修改
    36. cout << *it << " ";//打印2 2 3 4 5 5 6 出来有序+不去重
    37. ++it;
    38. }
    39. cout << endl;
    40. cout << s.count(5) << endl;//5出现的次数
    41. cout << s.erase(5) << endl;//删除所有5 返回删除了几个
    42. for (auto e : s)
    43. {
    44. cout << e << " ";//打印2 2 3 4 6
    45. }
    46. cout << endl;
    47. }

    set容器的查找

    1. //find 返回迭代器 不能修改
    2. void test_set2()
    3. {
    4. set<int> s;
    5. s.insert(5);
    6. s.insert(6);
    7. s.insert(2);
    8. s.insert(4);
    9. set<int>::iterator pos = s.find(2);//0(logN)
    10. if (pos != s.end())//set本身的find
    11. {
    12. cout << "set.find找到了" << endl;
    13. }
    14. pos = find(s.begin(), s.end(), 2);//0(N)
    15. if (pos != s.end())//算法当中的find
    16. {
    17. cout << "find找到了" << endl;
    18. }
    19. }

    set容器的删除

    1. //删除数据
    2. void test_set3()
    3. {
    4. set<int> s;
    5. s.insert(5);
    6. s.insert(6);
    7. s.insert(2);
    8. s.insert(4);
    9. s.insert(6);
    10. s.insert(9);
    11. int x;
    12. while (cin >> x)//持续输入
    13. {
    14. set<int>::iterator pos = s.find(x);
    15. if (pos != s.end())
    16. {
    17. s.erase(pos);
    18. cout << "删除" << x << "成功" << endl;
    19. }
    20. else
    21. {
    22. cout << x << "不在set中" << endl;
    23. }
    24. for (auto e : s)
    25. {
    26. cout << e << " ";
    27. }
    28. cout << endl;
    29. }
    30. }

    lower_bound和upper_bound 

    1. //lower_bound 返回>=位置的迭代器
    2. //upper_bound 返回>位置的迭代器
    3. void test_set4()
    4. {
    5. set<int> s;
    6. s.insert(5);
    7. s.insert(6);
    8. s.insert(2);
    9. s.insert(4);
    10. s.insert(9);
    11. for (auto e : s)
    12. {
    13. cout << e << " ";
    14. }
    15. cout << endl;
    16. //删除x<=[]<=y的区间
    17. int x, y;
    18. cin >> x >> y;
    19. auto left = s.lower_bound(x);
    20. auto right = s.upper_bound(y);//
    21. s.erase(left,right);
    22. for (auto e : s)
    23. {
    24. cout << e << " ";
    25. }
    26. cout << endl;
    27. }

    map容器的接口使用

    键值对一般只包含两个成员变量key和value,key代表键值,value表示与key对应的信息,比如词典中学生(key)对应student(value)

    SGI-STL中关于键值对的定义:

    1. template <class T1, class T2>
    2. struct pair
    3. {
    4. typedef T1 first_type;
    5. typedef T2 second_type;
    6. T1 first;
    7. T2 second;
    8. pair(): first(T1()), second(T2())
    9. {}
    10. pair(const T1& a, const T2& b): first(a), second(b)
    11. {}
    12. };

    map容器的初始化

    1. void test_map1()
    2. {
    3. mapdict;
    4. //调用pair 构造函数
    5. dict.insert(pair("sort", "排序"));
    6. //构造函数
    7. pairkv("insert", "插入");
    8. dict.insert(kv);
    9. //函数模板
    10. dict.insert(make_pair("left", "左边"));
    11. dict.insert(make_pair("left", "剩余"));
    12. }

    map容器的遍历

    1. //用map存一个小字典
    2. void test_map1()
    3. {
    4. mapdict;
    5. //调用pair 构造函数
    6. dict.insert(pair("sort", "排序"));
    7. //构造函数
    8. pairkv("insert", "插入");
    9. dict.insert(kv);
    10. //函数模板
    11. dict.insert(make_pair("left", "左边"));
    12. dict.insert(make_pair("left", "剩余"));
    13. //迭代器遍历遍历
    14. //map::iterator it = dict.begin();
    15. auto it = dict.begin();
    16. while (it != dict.end())
    17. {
    18. //pair (将key和value打包)
    19. cout << it->first << ":" << it->second << endl;
    20. ++it;
    21. }
    22. cout << endl;
    23. //auto 遍历
    24. for (auto& kv : dict)
    25. {
    26. cout << kv.first << ":" << kv.second << endl;
    27. }
    28. }

    map容器的查找

    1. //map支持[]操作符,在operator[]中实际进行插入查找
    2. //统计下面每个词语的次数
    3. void test_map2()
    4. {
    5. string arr[]{ "大学","中庸","论语","孟子","孔子"
    6. ,"大学","史记","论语" };
    7. mapint>countMap;
    8. for (auto& str : arr)
    9. {
    10. countMap[str]++;//统计次数 []返回的是value的引用
    11. //查找+修改 插入+修改
    12. }
    13. for (auto& kv : countMap)
    14. {
    15. cout << kv.first << ":" << kv.second << endl;
    16. }
    17. }

    operator[]的原理是:
    构造一个键值对,然后调用insert()函数将该键值对插入到map中
    如果key已经存在,插入失败,insert函数返回该key所在位置的迭代器
    如果key不存在,插入成功,insert函数返回新插入元素所在位置的迭代器
    operator[]函数最后将insert返回值键值对中的value返回

    学习set和map一定要学会查看文档,这里放一个官方文档链接map文档set文档,set和map在实际中非常的重要,我们只要熟悉常见的接口就可以

    希望这篇文章大家有所收获,我们下篇见

  • 相关阅读:
    【目标检测论文解读复现NO.34】基于改进 YOLOv5s 的苹果叶片小目标病害轻量化检测方法
    基于Apache Doris数仓平台架构设计
    绘画新手必备!六款免费易用的绘图软件推荐
    【Shell】sh执行脚本报错Syntax error: “(“ unexpected
    Java 之集合框架的详细介绍
    《算法导论》15.5 最优二叉搜索树(含C++代码)
    如何在Nuxt3.0中使用MongoDB数据库
    Python理论之一 —— 数据读写
    称砝码【set】
    ICC2:如何抓取“no net“的shape和via
  • 原文地址:https://blog.csdn.net/qq_72486849/article/details/126109704