• C++sort使用实例


    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    int cmp(int a,int b)
    {
        return a>b;
    }
    int main()
    {
        string str("hello world");
        sort(str.begin(),str.end());//string正序排列
        cout<<str<<endl;
        sort(str.rbegin(),str.rend());//string倒叙排列
        cout<<str<<endl;
        int a[5]={1,4,2,5,3};
        sort(a,a+5);//int数组正序排列
        for(int i=0;i<5;i++)
        {
            cout<<a[i]<<" ";
        }
        sort(a,a+5,cmp);//int数组倒叙排列
        for(int i=0;i<5;i++)
        {
            cout<<a[i]<<" ";
        }
        return 0;
     }

    1、sort函数可以三个参数也可以两个参数,必须的头文件#include < algorithm>和using namespace std; 

    2、Sort函数有三个参数:(第三个参数可不写)

    (1)第一个是要排序的数组的起始地址。

    (2)第二个是结束的地址(最后一位要排序的地址)。

    (3)第三个参数是排序的方法,可以是从大到小也可是从小到大,还可以不写第三个参数,此时默认的排序方法是从小到大排序。

  • 相关阅读:
    回调函数
    未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”包
    顶帽变化(转载)
    协程 + asyncio
    docker
    vue+uwsgi+nginx部署前后端分离项目
    html
    关于html的基础标签
    关于python中的GIL
    hashlib模块
  • 原文地址:https://www.cnblogs.com/deadpool520/p/11055469.html
Copyright © 2020-2023  润新知