• C++笔记打卡第23天(STL常用算法)


    1.常用排序算法

    • sort:对容器内元素进行排序

      1. class Myprint
      2. {
      3. public:
      4. void operator()(int val)
      5. {
      6. cout << val << " ";
      7. }
      8. };
      9. // 查自定义数据类型
      10. void test01()
      11. {
      12. vector<int> v;
      13. v.push_back(10);
      14. v.push_back(20);
      15. v.push_back(15);
      16. v.push_back(25);
      17. v.push_back(0);
      18. // 利用sort算法升序
      19. sort(v.begin(), v.end());
      20. for_each(v.begin(), v.end(), Myprint());
      21. cout << endl;
      22. // 降序
      23. sort(v.begin(), v.end(), greater<int>());
      24. for_each(v.begin(), v.end(), Myprint());
      25. cout << endl;
      26. }
    • random_shuffle:随机调整顺序

      1. #include
      2. void test01()
      3. {
      4. srand((unsigned int)time(NULL));
      5. vector<int> v;
      6. for(int i=0; i<10; i++)
      7. {
      8. v.push_back(i);
      9. }
      10. random_shuffle(v.begin(), v.end());
      11. for_each(v.begin(), v.end(), Myprint());
      12. cout << endl;
      13. }
    • merge:有序的两个容器元素合并,并存储到另一个容器中,输出仍然是有序的

      1. void test01()
      2. {
      3. vector<int> v1;
      4. vector<int> v2;
      5. for(int i=0; i<10; i++)
      6. {
      7. v1.push_back(i);
      8. v2.push_back(i+1);
      9. }
      10. vector<int> v3;
      11. v3.resize(v1.size() + v2.size());
      12. merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
      13. // 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
      14. }
    • reverse:反转指定范围的元素

      1. void test01()
      2. {
      3. vector<int> v1;
      4. for(int i=0; i<10; i++)
      5. {
      6. v1.push_back(i);
      7. }
      8. reverse(v1.begin(), v1.end()); // 9 8 7 6 5 4 3 2 1 0
      9. }

    2.常用拷贝和替换算法

    • copy:容器内指定范围的元素拷贝到另一容器中

      1. void test01()
      2. {
      3. vector<int> v1;
      4. for(int i=0; i<10; i++)
      5. {
      6. v1.push_back(i);
      7. }
      8. vector<int> v2;
      9. v2.resize(v1.size());
      10. copy(v1.begin(), v1.end(), v2.begin()); // 0 1 2 3 4 5 6 7 8 9
      11. }
    • replace:将容器内指定范围的旧元素修改为新元素

      1. void test01()
      2. {
      3. vector<int> v1;
      4. for(int i=0; i<10; i++)
      5. {
      6. v1.push_back(i);
      7. }
      8. // 将容器内所有的1替换为11
      9. replace(v1.begin(), v1.end(), 1, 11);
      10. }
    • replace_if:容器内指定范围满足条件的元素替换为新元素

      1. class Myprint
      2. {
      3. public:
      4. void operator()(int val)
      5. {
      6. cout << val << " ";
      7. }
      8. };
      9. class MyCompare
      10. {
      11. public:
      12. bool operator()(int val)
      13. {
      14. return val > 7;
      15. }
      16. };
      17. void test01()
      18. {
      19. vector<int> v1;
      20. for(int i=0; i<10; i++)
      21. {
      22. v1.push_back(i);
      23. }
      24. // 将大于等于7的替换为100
      25. replace_if(v1.begin(), v1.end(), MyCompare(), 100);
      26. }
    • swap:交换两个同种类型的容器内的元素

      1. void test01()
      2. {
      3. vector<int> v1;
      4. vector<int> v2;
      5. for(int i=0; i<10; i++)
      6. {
      7. v1.push_back(i);
      8. v2.push_back(i+100);
      9. }
      10. swap(v1, v2);
      11. }

    3.常用算术生成算法

    • 算术生成算法属于小型算法,使用时包含的头文件为 #include

    • accumulate:计算容器元素累计总和

      1. #include
      2. void test01()
      3. {
      4. vector<int> v1;
      5. for(int i=0; i<=100; i++)
      6. {
      7. v1.push_back(i);
      8. }
      9. // 从0开始将所有元素加起来
      10. int total = accumulate(v1.begin(), v1.end(), 0);
      11. cout << total << endl;
      12. }
    • fill:向容器中添加元素

      1. void test01()
      2. {
      3. vector<int> v1;
      4. v1.resize(10); // 10个0
      5. // 后期重新填充, 将范围内的值全改为100
      6. fill(v1.begin(), v1.end(), 100);
      7. for_each(v1.begin(),v1.end(), Myprint());
      8. cout << endl;
      9. }

    4.常用集合算法

    • set_intersection:求两个容器的交集

      1. void test01()
      2. {
      3. vector<int> v1;
      4. vector<int> v2;
      5. for(int i=0; i<10; i++)
      6. {
      7. v1.push_back(i);
      8. v2.push_back(i+2);
      9. }
      10. vector<int> v3;
      11. // 极限情况:一个容器是另一个容器的子集
      12. v3.resize(min(v1.size(), v2.size()));
      13. // 获取交集,返回末尾的迭代器
      14. vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
      15. for_each(v3.begin(), itEnd, Myprint());
      16. cout << endl;
      17. }
    • set_union:求两个容器的并集**(两个集合必须是有序的序列)**

      1. void test01()
      2. {
      3. vector<int> v1;
      4. vector<int> v2;
      5. for(int i=0; i<10; i++)
      6. {
      7. v1.push_back(i);
      8. v2.push_back(i+2);
      9. }
      10. vector<int> v3;
      11. // 极限情况:两个容器没有交集
      12. v3.resize(v1.size()+v2.size());
      13. // 获取交集,返回并集中最后一个元素的位置
      14. vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
      15. for_each(v3.begin(), itEnd, Myprint());
      16. cout << endl;
      17. }
    • set_difference:求两个容器的差集

      1. void test01()
      2. {
      3. vector<int> v1;
      4. vector<int> v2;
      5. for(int i=0; i<10; i++)
      6. {
      7. v1.push_back(i);
      8. v2.push_back(i+5);
      9. }
      10. vector<int> v3;
      11. v3.resize(max(v1.size(),v2.size()));
      12. cout << "v1和v2的差集为:" << endl;
      13. vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
      14. for_each(v3.begin(), itEnd, Myprint());
      15. cout << endl;
      16. cout << "v2和v1的差集为:" << endl;
      17. vector<int>::iterator itEnd2 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v3.begin());
      18. for_each(v3.begin(), itEnd2, Myprint());
      19. cout << endl;
      20. }
  • 相关阅读:
    【数据库】数据库的慢查询问题
    SpringBoot异常处理机制之自定义404、500错误提示页面
    优化sql提高查询效率
    SpringMVC参数注入解密
    maven下载jar包失败如何配置国内源(手把手)
    学习计算机视觉,必备六大知识点!(文末有惊喜)
    pip 清华镜像
    Blazor前后端框架Known-V1.2.14
    winform开启websocket
    影响MySql 服务性能最重要的两个参数。
  • 原文地址:https://blog.csdn.net/weixin_57139639/article/details/138199255