C++ multiset automatically sort person objects stored by pointer
#include <iostream> #include <set> #pragma warning (disable:4786)//from w w w. j a va 2 s.c o m #include <string> using namespace std; class person { 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&); void display() const // display person's data { cout << endl << lastName << ",\t" << firstName << "\t\tPhone: " << phoneNumber; } long get_phone() const // return phone number { return phoneNumber; } }; //end class person // overloaded < for person class 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; } // function object to compare persons using pointers class comparePersons { public: bool operator() (const person* ptrP1, const person* ptrP2) const { return *ptrP1 < *ptrP2; } }; int main() { // a multiset of ptrs to persons multiset<person*, comparePersons> setPtrsPers; multiset<person*, comparePersons>::iterator iter; //make persons person* ptrP1 = new person("K", "B", 4); person* ptrP2 = new person("M", "S", 3); person* ptrP3 = new person("D", "W", 8); person* ptrP4 = new person("W", "J", 9); person* ptrP5 = new person("B", "P", 6); person* ptrP6 = new person("M", "A", 8); person* ptrP7 = new person("F", "R", 7); person* ptrP8 = new person("M", "S", 7); setPtrsPers.insert(ptrP1); //put persons in multiset setPtrsPers.insert(ptrP2); setPtrsPers.insert(ptrP3); setPtrsPers.insert(ptrP4); setPtrsPers.insert(ptrP5); setPtrsPers.insert(ptrP6); setPtrsPers.insert(ptrP7); setPtrsPers.insert(ptrP8); //display multiset cout << "\n\nSet sorted when created:"; for(iter=setPtrsPers.begin(); iter != setPtrsPers.end(); iter++ ) (**iter).display(); iter = setPtrsPers.begin(); //delete all persons while( iter != setPtrsPers.end() ) { delete *iter; //delete person setPtrsPers.erase(iter++); //remove pointer } cout << endl; return 0; }