Source of stack02.cpp


  1: //stack02.cpp

  3: #include <iostream>
  4: #include <stack>
  5: #include <deque>
  6: #include <vector>
  7: using namespace std;

  9: int main()
 10: {
 11:     cout << "\nThis program illustrates the creation of a stack using "
 12:         "values from a deque\n(when the underlying container is the "
 13:         "default one, namely a deque), and from\na vector, when the "
 14:         "underlying container is a vector.";
 15:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 17:     int a1[] = {1, 2, 3, 4};
 18:     deque<int> d(a1, a1+4);
 19:     stack<int> s1(d);
 20:     cout << "\nThe stack s1 is created from a deque of 4 values.";
 21:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 23:     cout << "\nHere are the values of s1, in \"LIFO\" order:\n";
 24:     //This is the proper way to access the elements of a stack:
 25:     while(!s1.empty())
 26:     {
 27:         cout << "Popping: ";
 28:         cout << s1.top() << "\n";
 29:         s1.pop();
 30:     }
 31:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 33:     int a2[] = {6, 9, 8, 10, 5, 7};
 34:     vector<int> v(a2, a2+6);
 35:     stack<int, vector<int>> s2(v);
 36:     cout << "\nThe stack s2 is created from a vector of 6 values.";
 37:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 39:     cout << "\nHere are the values of s2, in \"LIFO\" order:\n";
 40:     while(!s2.empty())
 41:     {
 42:         cout << "Popping: ";
 43:         cout << s2.top() << "\n";
 44:         s2.pop();
 45:     }
 46:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
 47: }