题解 | 字符串排序

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
struct strR {
    int size;
    string s;
    strR(int size, string s): size(size), s(s) {}
    bool operator <(const strR r)const {
        return size < r.size;
    }
};
vector<strR> a;
string s;
int n;
int main() {
    while (cin >> n) {
        getchar();//因为有int输入在前,所以必须要有这一步
        while (n--) {
            getline(cin, s);
            if (s == "stop")break;
            a.push_back(strR(s.size(), s));
        }
        sort(a.begin(), a.end());
        for (auto x : a)cout << x.s << endl;
    }
    return 0;
}

简单的改写排序方法

全部评论

相关推荐

04-11 23:51
门头沟学院 Java
坚定的芭乐反对画饼_许愿Offer版:人人都能过要面试干嘛,发个美团问卷填一下,明天来上班不就好了
点赞 评论 收藏
分享
ResourceUtilization:四六级不愧是大学最有用的证之一
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务