给一个字符串数组排序,排序规则是同构字符串挨在一起。
思路:
重写sort函数的比较函数,然后调用sort函数即可。
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; bool cmp(string s1, string s2) { sort(&s1[0], &s1[0] + s1.size()); sort(&s2[0], &s2[0] + s2.size()); return s1 < s2; } void Sort(vector<string>& svec) { sort(svec.begin(), svec.end(), cmp); for (int i = 0; i < svec.size(); ++i) cout << svec[i] << endl; } int main() { vector<string> svec; svec.push_back("axyz"); svec.push_back("cab"); svec.push_back("zyxa"); svec.push_back("bac"); svec.push_back("gf"); svec.push_back("xyza"); svec.push_back("fg"); Sort(svec); return 0; }