C++ multiset hold person objects
#include <iostream> #include <set> #include <string> using namespace std; class person/*from ww w. j a v a 2 s . com*/ { private: string lastName; string firstName; long phoneNumber; public: // default constructor person() : lastName("blank"), firstName("blank"), phoneNumber(0) { } // 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; } }; // operator < 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; } // operator == for person class bool operator==(const person& p1, const person& p2) { return (p1.lastName == p2.lastName && p1.firstName == p2.firstName ) ? true : false; } int main() { person pers1("D", "W", 8); person pers2("M", "S", 3); person pers3("B", "P", 6); person pers4("K", "B", 4); person pers5("W", "J", 9); person pers6("M", "A", 8); person pers7("F", "R", 7); person pers8("M", "S", 2); multiset< person, less<person> > persSet; // iterator to a multiset of persons multiset<person, less<person> >::iterator iter; persSet.insert(pers1); // put persons in multiset persSet.insert(pers2); persSet.insert(pers3); persSet.insert(pers4); persSet.insert(pers5); persSet.insert(pers6); persSet.insert(pers7); persSet.insert(pers8); cout << "\nNumber of entries = " << persSet.size(); iter = persSet.begin(); // display contents of multiset while( iter != persSet.end() ) (*iter++).display(); // get last and first name string searchLastName, searchFirstName; cout << "\n\nEnter last name of person to search for: "; cin >> searchLastName; cout << "Enter first name: "; cin >> searchFirstName; // create person with this name person searchPerson(searchLastName, searchFirstName, 0); // get count of such persons int cntPersons = persSet.count(searchPerson); cout << "Number of persons with this name = " << cntPersons; // display all matches iter = persSet.lower_bound(searchPerson); while( iter != persSet.upper_bound(searchPerson) ) (*iter++).display(); cout << endl; return 0; }