题解 | 字符串排序
#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; }
简单的改写排序方法