首页 > 代码库 > c++ list sort

c++ list sort

// list::sort#include <iostream>#include <list>#include <string>#include <cctype>// comparison, not case sensitive.bool compare_nocase (const std::string& first, const std::string& second){  unsigned int i=0;  while ( (i<first.length()) && (i<second.length()) )  {    if (tolower(first[i])<tolower(second[i])) return true;    else if (tolower(first[i])>tolower(second[i])) return false;    ++i;  }  return ( first.length() < second.length() );}int main (){  std::list<std::string> mylist;  std::list<std::string>::iterator it;  mylist.push_back ("one");  mylist.push_back ("two");  mylist.push_back ("Three");  mylist.sort();  std::cout << "mylist contains:";  for (it=mylist.begin(); it!=mylist.end(); ++it)    std::cout <<   << *it;  std::cout << \n;  mylist.sort(compare_nocase);  std::cout << "mylist contains:";  for (it=mylist.begin(); it!=mylist.end(); ++it)    std::cout <<   << *it;  std::cout << \n;  return 0;}

 以上是用string 模版写的可以自定义方法对list.sort();

可参考http://www.cplusplus.com/reference/list/list/sort/

 

2.list sort 模版如何使用

 

void sort( ); template<class Traits>    void sort(      Traits _Comp   );
可参考http://technet.microsoft.com/zh-cn/library/kz841ss7(v=vs.90)

3.如果一个list 放的student 包含name(string) age(int).
如何用list.sort()方法分别排序?

c++ list sort