简单的字符串排序,用到了sort()函数,关键在于cmp规则的编写
#include <iostream> #include <cstring> #include <string> #include <algorithm> using namespace std; #define _for(i, a, b) for(int i = a; i < b; i++) bool cmp(string a, string b){ int lena = a.length(), lenb = b.length(); if(lena != lenb) return lena < lenb; return a < b; } int main(){ int n; cin >> n; string s[110]; _for(i, 0, n){ cin >> s[i]; } sort(s, s + n, cmp); _for(i, 0, n){ cout << s[i] << endl; } return 0; }