数据结构十三、set & map

[复制链接]
发表于 2025-3-27 21:39:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
一、set

1、size / empty

        size:返回set中实际元素的个数
        empty:判定set是否为空
2、begin / end

        这是两个迭代器,因此可以使用范围for来遍历整个红黑树。其中,遍历是按照中序遍历的顺序,因此是一个有序序列。
3、insert / erase

        insert:向红黑树中插入一个元素
        erase:删除一个元素
4、find / count

        find:查找一个元素,返回的是迭代器
        count:查询元素出现的次数
假如想查找元素是否在set中,我们一样寻常不使用find,而是使用count。因为find的返回值是一个迭代器,判定起来不方便。
5、lower_bound / upper_bound

        lower_bound:大于等于x的最小元素,返回的是迭代器
        upper_bound:大于x的最小元素,返回的是迭代器
6、全部测试代码

  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4. int a[] = { 10,60,20,70,80,30,90,40,100,50 };
  5. int main()
  6. {
  7.         set<int> mp;
  8.         for (auto e : a)
  9.         {
  10.                 mp.insert(e);
  11.         }
  12.         for (auto x : mp)
  13.         {
  14.                 cout << x << " ";
  15.         }
  16.         cout << endl;
  17.         if (mp.count(1))
  18.                 cout << "1" << endl;
  19.         if (mp.count(10))
  20.                 cout << "10" << endl;
  21.         mp.erase(10);
  22.         if (mp.count(10))
  23.                 cout << "YES" << endl;
  24.         else
  25.                 cout << "NO" << endl;
  26.         auto x = mp.lower_bound(20);
  27.         auto y = mp.upper_bound(20);
  28.         cout << *x << " " << *y << endl;
  29.         return 0;
  30. }
复制代码
二、map

map与set的区别:set内里存储的是一个单独的关键字(也就是存一个int、char、double或者string)。而map内里存的是一个pair<key, value>(k-v模型),不仅有一个关键字,还有一个与关键字绑定的值,比较方式是按照key的值来比较。好比,我们可以在map中存<string, int>来统计字符串出现的次数。
1、insert

        向红黑树中插入一个元素。这里必要一个pair,可以用{}的情势。
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4. void print(map<string, int>& mp)
  5. {
  6.         for (auto& p : mp)
  7.         {
  8.                 cout << p.first << " " << p.second << endl;
  9.         }
  10. }
  11. int main()
  12. {
  13.         map<string, int> mp;
  14.         mp.insert({ "zhangsan", 1 });
  15.         mp.insert({ "lisi", 2 });
  16.         mp.insert({ "wangwu", 3 });
  17.         print(mp);
  18.         return 0;
  19. }
复制代码
2、operator[]

        重载[],使得map可以像数组一样使用。
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4. void print(map<string, int>& mp)
  5. {
  6.         for (auto& p : mp)
  7.         {
  8.                 cout << p.first << " " << p.second << endl;
  9.         }
  10. }
  11. int main()
  12. {
  13.         map<string, int> mp;
  14.         mp.insert({ "zhangsan", 1 });
  15.         mp.insert({ "lisi", 2 });
  16.         mp.insert({ "wangwu", 3 });
  17.         print(mp);
  18.         cout << mp["zhangsan"] << endl;
  19.         mp["zhangsan"] = 110;
  20.         cout << mp["zhangsan"] << endl;
  21.         return 0;
  22. }
复制代码
三、例题


 算法原理:求出最小波动值,我们只必要针对某一天的业务额,找出前面的数中哪个数离它最近(大于等于x的最小值 或者 小于等于x的最大值)。前一种环境可以调用lower_bound函数,后一种环境可以使用迭代器来解决。
  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4. const int INF = 1e7 + 10;
  5. int n;
  6. set<int> mp;
  7. int main()
  8. {
  9.         cin >> n;
  10.         int ret;
  11.         cin >> ret;
  12.         mp.insert(ret);
  13.         mp.insert(-INF);
  14.         mp.insert(INF);
  15.         for (int i = 2;i <= n;i++)
  16.         {
  17.                 int x;
  18.                 cin >> x;
  19.                 auto it = mp.lower_bound(x);
  20.                 auto tmp = it;
  21.                 tmp--;
  22.                 if (*it == x)
  23.                         continue;
  24.                 ret += min(abs(*tmp - x), abs(*it - x));
  25.                 mp.insert(x);
  26.         }
  27.         cout << ret << endl;
  28.         return 0;
  29. }
复制代码

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表