C++——STL 常用的排序算法

算法简介:

  • sort // 对容器内元素进行排序
  • reandom_shuffle // 洗牌 指定范围内的元素随机调整次序
  • merge // 容器元素合并,并存储到另一个容器中
  • reverse // 反转指定范围的元素

1. sort

  • 功能描述:

    • 对容器内元素进行排序
  • 函数原型:

    sort(iterator beg, iterator end, _Perd);

// beg 开始迭代器

// end 结束迭代器

// _Pred 谓词

  • 示例:

    #include<iostream>
    using namespace std;
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <functional>

    // 常用排序算法 sort
    void myPrint(int val){
    cout << val << " ";
    }

    void test01(){
    vector<int>v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);
    // 利用sort进行升序
    sort(v.begin(),v.end());
    for_each(v.begin(),v.end(),myPrint);
    cout << endl;
    // 降序
    sort(v.begin(),v.end(),greater<int>());
    for_each(v.begin(),v.end(),myPrint);
    cout << endl;
    }

    int main(){
    test01();
    return 0;
    }
    // 10 20 30 40 50
    // 50 40 30 20 10

2. random_shuffle

  • 功能描述

    • 洗牌:指定范围内的元素随机调整次序
  • 函数原型:

    random_shuffle(iterator beg, iterator end);

//beg 开始迭代器

//end 结束迭代器

  • 示例:

    #include<iostream>
    using namespace std;
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <functional>
    #include <ctime>
    // 常用排序算法 random_shuffle
    void myPrint(int val){
    cout << val << " ";
    }
    void test01(){
    vector<int>v;
    for (int i = 0; i < 10; i++){
    v.push_back(i);
    }
    random_shuffle(v.begin(),v.end());
    for_each(v.begin(),v.end(),myPrint);
    cout << endl;
    }

    int main(){
    srand((unsigned int)time(NULL));
    test01();
    return 0;
    }

3. mrege

  • 功能描述:

    • 两个容器合并,并存储到另一个容器中
  • 函数原型:

    merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

// 注意:两个容器必须是有序的

// beg1 容器1开始迭代器

// end1 容器1结束迭代器

// beg2 容器2开始迭代器

// end2 容器2结束迭代器

// dest 目标容器开始迭代器

  • 示例:

    #include<iostream>
    using namespace std;
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <functional>
    // 常用排序算法 merge
    void myPrint(int val){
    cout << val << " ";
    }
    void test01(){
    vector<int>v1;
    vector<int>v2;
    for (int i = 0; i < 10; i++){
    v1.push_back(i);
    v2.push_back(i+1);

    复制代码
      }
      vector<int>vTarget;
      vTarget.resize(v1.size() + v2.size());
      merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
      for_each(vTarget.begin(),vTarget.end(),myPrint);
      cout << endl;

    }

    int main(){
    test01();
    return 0;
    }
    //0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10

4. reverse

  • 功能描述:

    • 将容器内元素进行反转
  • 函数原型:

    reverse(ierator beg, iterator end);

// beg 开始迭代器

// end 结束迭代器

  • 示例:

    #include<iostream>
    using namespace std;
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <functional>
    // 常用排序算法 reverse
    class myPrint
    {
    public:
    void operator()(int val){
    cout << val << " ";
    }
    };
    void test01(){
    vector<int>v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);
    cout << "反转前:" << endl;
    for_each(v.begin(),v.end(),myPrint());
    cout << endl;
    cout << "反转后" << endl;
    reverse(v.begin(), v.end());
    for_each(v.begin(),v.end(),myPrint());
    cout << endl;
    }
    int main(){
    test01();
    return 0;
    }
    // 反转前:
    // 10 30 50 20 40
    // 反转后:
    // 40 20 50 30 10

相关推荐
金融数据出海22 分钟前
使用 PHP 和 Guzzle 对接印度股票数据源API
开发语言·spring boot·金融·区块链·php
楠目22 分钟前
JS语言基础
开发语言·前端·javascript
代码老y1 小时前
C语言进阶知识:深入探索编程的奥秘
c语言·开发语言·数据结构·算法
钢铁男儿1 小时前
C# 类和继承(构造函数的执行)
java·开发语言·c#
feiyangqingyun1 小时前
Qt/C++编写GB28181服务端工具/绿色版开箱即用/对标wvp-gb28181/实时画面预览/录像回放下载
c++·qt·gb28181·监控系统开发
feiyangqingyun1 小时前
关于无法下载Qt离线安装包的说明
开发语言·qt
三体世界1 小时前
Linux --TCP协议实现简单的网络通信(中英翻译)
linux·c语言·开发语言·网络·c++·windows·tcp/ip
苕皮蓝牙土豆1 小时前
Qt信号与槽机制深度解析
开发语言·qt
苕皮蓝牙土豆2 小时前
Qt概述:基础组件的使用
开发语言·qt
zh_xuan2 小时前
java Semaphore‌
java·开发语言