Mega Code Archive

 
Categories / C++ / Data Structure
 

Using a list to store mailing addresses

#include <iostream> #include <list> #include <string> using namespace std; class Address {   string name;   string street;   string city;   string state;   string zip; public:   Address() {     name = street = city = state = zip = "";   }   Address(string n, string s, string c, string st, string z) {     name = n;     street = s;     city = c;     state = st;     zip = z;   }   string getname() {       return name;    }   string getcity() {       return city;    }   string getstreet() {       return street;    }   string getstate() {       return state;    }   string getzip() {       return zip;    } }; // List sorted by name. bool operator<(Address &a, Address &b) {   return a.getname() < b.getname(); } // List searched by name. bool operator==(Address &a, Address &b) {   return a.getname() == b.getname(); } void display(list<Address> &listObject) {   list<Address>::iterator p;   for(p = listObject.begin(); p != listObject.end(); p++) {     cout << p->getname() << ": ";     cout << p->getstreet() << ", ";     cout << p->getcity() << ", ";     cout << p->getstate() << " ";     cout << p->getzip() << endl;   } } int main() {   list<Address> mlistObjectA, mlistObjectB;   mlistObjectA.push_back(Address("J", "1102 St", "Mission", "TX", "78572"));    mlistObjectA.sort();   mlistObjectB.sort();   // merge mailings lists   mlistObjectA.merge(mlistObjectB);   cout << "List A after sorting and merging.\n";   display(mlistObjectA);   cout << "List A now has " << mlistObjectA.size();   cout << " entries.\n";   // remove duplicates   mlistObjectA.unique();   cout << "List A after removing duplicates.\n";   display(mlistObjectA);   cout << "List A now has " << mlistObjectA.size();   cout << " entries.\n";   return 0; }