bool Comp(const string& s1, const string& s2){
return (s1.length()!=s2.length() ? s1.length()<s2.length() : s1<s2);
}
int main(int argc, char* argv[])
{
//ifstream cin("test.txt");
// ifstream cin("a.txt");
//calculate and store first
vector<string> vs;
string s,t;
while (cin>>s)
{
t=s;
reverse(t.begin(), t.end());
if (t==s)
{
vs.push_back(s);
}
}
sort(vs.begin(), vs.end(), Comp);
for (vector<string>::iterator it=vs.begin(); it!=vs.end();it++)
{
cout<<*it<<endl;
}
return 0;
}
注意使用自动排序的stl结构set,map和使用自定义sort方法的不同之处,
前者是重载操作符,后者是定义比较函数。