Using a list to store mailing addresses. : List « Data Structure « C++






Using a list to store mailing addresses.

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;
}



           
       








Related examples in the same category

1.List basics: push, begin, end popList basics: push, begin, end pop
2.Elements can be put on the front or end of a list.Elements can be put on the front or end of a list.
3.Define class and store in a listDefine class and store in a list
4.An example of the transform algorithm.An example of the transform algorithm.
5.Demonstrate merge() in listDemonstrate merge() in list
6.Merging won't work if the lists aren't ordered.Merging won't work if the lists aren't ordered.
7.Merge into descending order.Merge into descending order.
8.A list splicing example.A list splicing example.
9.Demonstrate remove() in listDemonstrate remove() in list
10.Demonstrate unique() in listDemonstrate unique() in list
11.Using reverse() to create a palindrome tester.Using reverse() to create a palindrome tester.
12.Use ostream_iterator for stringUse ostream_iterator for string
13.Demonstrate advance() and distance() in listDemonstrate advance() and distance() in list
14.Using an arrays as a containerUsing an arrays as a container
15.Using a list: push_back, begin, end, sizeUsing a list: push_back, begin, end, size
16.Merge and splice lists.Merge and splice lists.
17.Copy a list to a vector.Copy a list to a vector.
18.Push value in the listPush value in the list
19.Use a binary function object in list 'transform'Use a binary function object in list 'transform'
20.end() in list.end() in list.
21.The difference between push_back() and push_front().The difference between push_back() and push_front().
22.Sort a list.Sort a list.
23.Merge two lists.Merge two lists.
24.Store class objects in a list.Store class objects in a list.
25.Transform algorithm based on list.Transform algorithm based on list.
26.Demonstrate bind2nd().Demonstrate bind2nd().
27.Create a reciprocal function object.Create a reciprocal function object.
28.Understanding end() in ListUnderstanding end() in List
29.Difference between push_back() and push_front()Difference between push_back() and push_front()
30.Merging One List with AnotherMerging One List with Another
31.Storing Class Objects in a ListStoring Class Objects in a List
32.Traverse a List Using an IteratorTraverse a List Using an Iterator
33.Demonstrate virtual functons: list interfaceDemonstrate virtual functons: list interface