string类型字符串排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lzc842650834/article/details/72322909

大名鼎鼎的sort函数出场了,它是在一个比较难记的头文件里#include <algorithm>(记性差怪我喽)
对于string的升序排序很简单,直接调用sort函数即可:

string a = "abc";
sort(a.begin(), a.end());

但是如果我想用降序排序呢?其实sort函数还有第三个参数,通过我们自己编写一个函数,即可降序排序:

#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(char a, char b)
{
    return a > b;
}
int main()
{
    string a = "abc";
    sort(a.begin(), a.end(), cmp);
    cout << a;
    return
{

这就大功告成啦,如果想了解的更深刻的小伙伴,自行谷歌欧~

猜你喜欢

转载自blog.csdn.net/lzc842650834/article/details/72322909
今日推荐