Source of list16.cpp


  1: //list16.cpp

  3: #include <iostream>
  4: #include <list>
  5: using namespace std;

  7: bool bothDivisibleByThree
  8: (
  9:     int m, //in
 10:     int n  //in
 11: )
 12: /**<
 13: Determine if two integers are both divisible by 3.
 14: @return true if both input integers are divisible by 3, and
 15: otherwise false.
 16: @param m An integer.
 17: @param n An integer.
 18: @pre m and n both contain an integer.
 19: @post Returns 
 20: */
 21: {
 22:     return (m%3 == 0) && (n%3 == 0);
 23: }

 25: int main()
 26: {
 27:     cout << "\nThis program illustrates the unique() "
 28:         "member function for list objects.";
 29:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 31:     int a[] = {1, 2, 2, 3, 6, 5, 2, 6, 9, 12, 2, 2, 2, 9, 10, 11, 12};
 32:     list<int> lst(a, a+17);
 33:     cout << "\nFor the starting list we have:";
 34:     cout << "\nSize = " << lst.size();
 35:     cout << "\nContents: ";
 36:     list<int>::iterator p = lst.begin();
 37:     while (p!= lst.end())  cout << *p++ << " ";
 38:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 40:     cout << "\nNow we remove all adjacent duplicate copies of "
 41:         "\nthe value 2 and redisplay the list to confirm:";
 42:     lst.unique();
 43:     p = lst.begin();
 44:     cout << "\nSize = " << lst.size();
 45:     cout << "\nContents: ";
 46:     while (p!= lst.end())  cout << *p++ << " ";
 47:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 49:     cout << "\nFinally we remove all adjacent duplicate values "
 50:         "\ndivisible by 3 and redisplay the list to confirm:";
 51:     lst.unique(bothDivisibleByThree);
 52:     p = lst.begin();
 53:     cout << "\nSize = " << lst.size();
 54:     cout << "\nContents: ";
 55:     while (p!= lst.end())  cout << *p++ << " ";
 56:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 57: }