C++标准模板库(STL)——sort()

sort是用来排序的函数,效率较高。

1.如何使用sort函数

必须加上头文件:"include <algorithm>"和"using namespace;"

使用方法如下:

sort(首元素地址(必填),尾元素地址的下一个地址(必填),比较函数(非必填));

默认对前面的区间进行递增排序;

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a[6]={9,4,2,5,6,-1};
    //将a[0]~a[3]从小到大排序
    sort(a,a+4);
    for(int i=0;i<6;i++)
    cout << a[i] << ' ';
    cout << endl;

    //将所有数排序
    sort(a,a+6);
    for(int i=0;i<6;i++)
    cout << a[i] << ' ';
    cout << endl; 
    return 0;
}

C++标准模板库(STL)——sort()

 

 double型数组排序同int型;

对char型数组排序——默认为字典序

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    char c[]={'T','W','A','K','I','Y'};
    sort(c,c+6);
    for(int i=0;i<6;i++)
    cout << c[i] << ' ';
    cout << endl;
    return 0;
}

C++标准模板库(STL)——sort()

 

 注意:如果需要对序列进行排序,那么序列中的元素一定要具有可比性;特别是结构体,需要人为制定比较规则。

2.如何实现比较函数cmp()——compare函数

1)基本数据类型数组的排序

若比较函数不填,则默认从小到大的顺序排列

如果想要从大到小排列,就需要使用比较函数cmp来告诉sort合适要交换元素。

#include<iostream>
#include<algorithm>
using namespace std;

bool cmp(int a,int b)
{
    return a>b; //可以理解为当a>b时,将a放在b前 
}

int main()
{
    int a[6]={9,4,2,5,6,-1};
    sort(a,a+6,cmp);
    for(int i=0;i<6;i++)
    cout << a[i] << ' ';
    cout << endl; 
    return 0;
}

C++标准模板库(STL)——sort()

 

 double型,char型数组排序同int型;

2)结构体数组的排序

#include<iostream>
#include<algorithm>
using namespace std;

struct node{
    int x,y;
}ssd[10];

bool cmp(node a,node b)
{
    if(a.x!=b.x)
    return a.x>b.x;
    else   //二级排序:当x相等时按y从大到小排序 
    return a.y>b.y;
}

int main()
{
    ssd[0].x=2;
    ssd[0].y=2;
    ssd[1].x=1;
    ssd[1].y=3;
    ssd[2].x=2;
    ssd[2].y=1;
    sort(ssd,ssd+3,cmp);
    for(int i=0;i<3;i++)
    cout << ssd[i].x << ' ' << ssd[i].y << endl; 
    return 0;
}

C++标准模板库(STL)——sort()

 

 C++标准模板库(STL)——sort()

 

 3)容器的排序

在STL标准容器中,只有vector,string,deque可以使用sort的。

以vector为例:

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool cmp(int a,int b)
{
    return a>b;
}

int main()
{
    vector<int> vi;
    vi.push_back(3);
    vi.push_back(1);
    vi.push_back(2);
    sort(vi.begin(),vi.end(),cmp);  //对整个vector排序
    for(int i=0;i<3;i++)
    cout << vi[i] << ' ';
    cout << endl; 
    return 0;
}

C++标准模板库(STL)——sort()

 

 string 排序

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    string str[3]={"bbbb","cc","aaa"};
    sort(str,str+3);
    for(int i=0;i<3;i++)
    cout << str[i] << endl;
    return 0;
}

C++标准模板库(STL)——sort()

 

 字符串长度排序

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool cmp(string a,string b)
{
    return a.length()<b.length();
}
int main()
{
    string str[3]={"C加加","游戏","编程学习"};
    sort(str,str+3,cmp);
    for(int i=0;i<3;i++)
    cout << str[i] << endl;
    return 0;
}

C++标准模板库(STL)——sort()

 

原文链接: https://www.cnblogs.com/wlyperfect/p/12504735.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++标准模板库(STL)——sort()

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/335675

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午10:15
下一篇 2023年3月1日 下午10:16

相关推荐