ccplusplus.com
Learn C, C++ Concepts
Wednesday, January 11, 2012
binary predicates stl example
/*********************************************************************** * File : binary-predicate.cpp * Author : Saurabh Gupta * Desc : binary predicates stl example * Source : http://ccplusplus/2011/10/standard-template-library.html * Created : PM 10:18 11 January 2012 * Note : The program is just a prototype, it won't compile. * change the program according to your requirement ************************************************************************/ #include <iostream> #include <string> #include <deque> #include <algorithm> using namespace std; class Person { public: string firstname() const; string lastname() const; /* * add your variable according to requirement */ }; /* binary function predicate: * - returns whether a person is less than another person */ bool personSortCriterion (const Person& p1, const Person& p2) { /* a person is less than another person * - if the last name is less * - if the last name is equal and the first name is less */ return p1.lastname()<p2.1astname() || (!(p2.1astname()<p1.lastname()) && p1.firstname()<p2.firstname()); } int main() { deque<Person> coll; /* * create a deque to hold the person details */ sort (coll. begin(), coll. end() , //range personSortCriterion); //sort criterion /* * perform your computation here */ return 0; }
See Also:
Standard Template Library Concepts and sample codes in C++
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment