Sorts person objects stored by pointer - C++ STL Algorithm

C++ examples for STL Algorithm:sort

Description

Sorts person objects stored by pointer

Demo Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
class person//from   www.  j  a  va  2 s . c  o m
{
   private:
   string lastName;
   string firstName;
   long phoneNumber;
   public:
   person() :             // default constructor
   lastName("blank"), firstName("blank"), phoneNumber(0L)
   {  }
   // 3-arg constructor
   person(string lana, string fina, long pho) :
   lastName(lana), firstName(fina), phoneNumber(pho)
   {  }
   friend bool operator<(const person&, const person&);
   friend bool operator==(const person&, const person&);
   void display() const   // display person's data
   {
      cout << endl << lastName << ",\t" << firstName
      << "\t\tPhone: " << phoneNumber;
   }
   long get_phone() const // return phone number
   { return phoneNumber; }
};

bool operator<(const person& p1, const person& p2)
{
   if(p1.lastName == p2.lastName)
      return (p1.firstName < p2.firstName) ? true : false;
   return (p1.lastName < p2.lastName) ? true : false;
}
// overloaded == for person class
bool operator==(const person& p1, const person& p2)
{
   return (p1.lastName == p2.lastName &&
   p1.firstName == p2.firstName ) ? true : false;
}
// function object to compare persons using pointers
class comparePersons
{
   public:
   bool operator() (const person* ptrP1,
   const person* ptrP2) const
   { return *ptrP1 < *ptrP2; }
};
//function object to display a person, using a pointer
class displayPerson
{
   public:
   void operator() (const person* ptrP) const
   { ptrP->display(); }
};
int main()
{
    vector<person*> vectPtrsPers;
    //make persons
    person* ptrP1 = new person("K", "B", 4);
    person* ptrP2 = new person("D", "W", 8);
    person* ptrP3 = new person("W", "J", 9);
    person* ptrP4 = new person("B", "P", 6);
    person* ptrP5 = new person("F", "R", 7);
    person* ptrP6 = new person("M", "S", 7);
    vectPtrsPers.push_back(ptrP1);  //put persons in set
    vectPtrsPers.push_back(ptrP2);
    vectPtrsPers.push_back(ptrP3);
    vectPtrsPers.push_back(ptrP4);
    vectPtrsPers.push_back(ptrP5);
    vectPtrsPers.push_back(ptrP6);
    for_each(vectPtrsPers.begin(),vectPtrsPers.end(), displayPerson() );//display vector
    //sort pointers
    sort( vectPtrsPers.begin(), vectPtrsPers.end() );
    cout << "\n\nSorted pointers";
    for_each(vectPtrsPers.begin(), vectPtrsPers.end(), displayPerson() );//display vector
    sort( vectPtrsPers.begin(),vectPtrsPers.end(), comparePersons() );//sort persons
    cout << "\n\nSorted persons";
    for_each(vectPtrsPers.begin(),vectPtrsPers.end(), displayPerson() );//display vector
    while( !vectPtrsPers.empty() )
    {
       delete vectPtrsPers.back();               //delete person
       vectPtrsPers.pop_back();                  //pop pointer
    }
    cout << endl;
    return 0;
}

Result


Related Tutorials