65,186
社区成员




#include <string>
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;
bool op(string str1, string str2)
{
return str1.length() < str2.length();
}
int main(int argc, char* argv[])
{
list <string> list_story;
list_story.push_back("the");
list_story.push_back("quick");
list_story.push_back("red");
list_story.push_back("fox");
list_story.push_back("jumps");
list_story.push_back("over");
list_story.push_back("the");
list_story.push_back("slow");
list_story.push_back("red");
list_story.push_back("turtle");
list_story.sort(op);
for(list<string>::iterator i = list_story.begin(); i != list_story.end(); i++)
{
cout << *i << endl;
}
}
#include <string>
#include <list>
#include <iostream>
using namespace std;
bool cmp(const string& s1,const string& s2);
int main()
{
list <string> list_story;
list_story.push_back("the");
list_story.push_back("quick");
list_story.push_back("red");
list_story.push_back("fox");
list_story.push_back("jumps");
list_story.push_back("over");
list_story.push_back("the");
list_story.push_back("slow");
list_story.push_back("red");
list_story.push_back("turtle");
for (list<string>::iterator iter = list_story.begin();iter != list_story.end();iter++)
{
cout<<*iter<<endl;
}
cout<<"sort after"<<endl;
list_story.sort(cmp);
for (list<string>::iterator iter = list_story.begin();iter != list_story.end();iter++)
{
cout<<*iter<<endl;
}
system("pause");
}
bool cmp(const string& s1,const string& s2)
{
return s1.length() < s2.length();
}