//sortList.cpp --- demo using list::sort //from http://www.cplusplus.com/reference/stl/list/sort.html // modified by tsaiwn@csie.nctu.edu.tw #include #include #include #include #include using namespace std; // comparison, not case sensitive. bool compare_nocase(string first, string second) { unsigned int i=0; while ( (itolower(second[i])) return false; ++i; } if (first.length() void printList(list); int main ( ) { list aList; aList.push_back ("one"); aList.push_back ("two"); aList.push_back ("Three"); aList.push_back("Four"); aList.push_back("five"); aList.push_front("ZERO_haha"); aList.push_back("six"); cout << "mylist contains:"; printList(aList); aList.sort(); // Note that there is NO function sort(list) in C++ STL cout << "After sort( ),\nmylist contains:"; printList(aList); aList.sort(compare_nocase); cout << "After sort(compare_nocase),\nmylist contains:"; printList(aList); double gg[ ] = {12.3, 45.6, 38.5, 88, 55.5}; list ggLL(gg, gg+(sizeof gg/sizeof gg[0])); cout << "ggLL: "; printList(ggLL); ggLL.sort( ); cout << "after ggLL.sort(): "; printList(ggLL); return 0; }// main( // use an iterator to traverse a Linked List template void printList(list myList) { typename list::iterator it; // note the "typename" for (it=myList.begin( ); it != myList.end( ); ++it) cout << " " << *it; // iterator is actually a pointer cout << endl; } /****** C:\testc\STL>g++ sortList.cpp C:\testc\STL>a mylist contains: ZERO_haha one two Three Four five six After sort( ), mylist contains: Four Three ZERO_haha five one six two After sort(compare_nocase), mylist contains: five Four one six Three two ZERO_haha ggLL: 12.3 45.6 38.5 88 55.5 after ggLL.sort(): 12.3 38.5 45.6 55.5 88 *******************************/