[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppSort

(C++) std::sort

STL algorithm for sorting. Can be found in the header file <algorithm>.

Manual ways of sorting:
  1. include <iostream>
  2. include <vector>
  3. include <string>
  4. include <algorithm>
struct SortOnSecondLetter { bool operator()(const std::string& lhs, const std::string& rhs) { return (lhs.substr(1,2) < rhs.substr(1,2)); } }; struct Couter { void operator()(const std::string& s) { std::cout << s << std::endl; } }; int main() { std::vector<std::string> v; v.push_back("acb"); v.push_back("cbc"); v.push_back("bac"); v.push_back("baa"); std::cout << "Before sort: " << std::endl; std::for_each(v.begin(), v.end(), Couter()); std::sort(v.begin(),v.end(),SortOnSecondLetter()); std::cout << "After sort: " << std::endl; std::for_each(v.begin(), v.end(), Couter()); }


'Sort' links

Code links



last edited (November 16, 2006) by bilderbikkel, Number of views: 2718, Current Rev: 6 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.