Source of deque01.cpp


  1: //deque01.cpp

  3: #include <iostream>
  4: #include <string>
  5: #include <deque>
  6: using namespace std;

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates all constructors of the STL "
 11:         "deque container class,\nas well as the empty() and size() "
 12:         "member functions, the size_type typedef,\nand the d[i] method "
 13:         "of accessing deque components.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 16:     //Create an empty deque
 17:     deque<int> d1;
 18:     if (d1.empty()) cout << "\nd1 is empty.\n";
 19:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 21:     //Create a deque filled with the default component-type value
 22:     deque<int> d2(5);
 23:     cout << "\nContents of d2: ";
 24:     for (deque<int>::size_type i=0; i<d2.size(); i++)
 25:         cout << d2[i] << " ";
 26:     cout << endl;
 27:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 29:     //Create a deque filled with a specific component-type value
 30:     deque<double> d3(4, 3.14);
 31:     cout << "\nContents of d3: ";
 32:     for (deque<double>::size_type i=0; i<d3.size(); i++)
 33:         cout << d3[i] << " ";
 34:     cout << endl;
 35:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 37:     //Create a deque using pointers to an integer array
 38:     int a[] = {2, 4, 6, 8, 10, 12};
 39:     deque<int> d4(a, a + sizeof(a)/sizeof(int));
 40:     cout << "\nContents of d4: ";
 41:     for (deque<int>::size_type i=0; i<d4.size(); i++)
 42:         cout << d4[i] << " ";
 43:     cout << endl;
 44:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 46:     //Create a deque using pointers to a C-string
 47:     char s[] = "Hello";
 48:     deque<char> d5(s, s + strlen(s));
 49:     cout << "\nContents of d5: ";
 50:     for (deque<char>::size_type i=0; i<d5.size(); i++)
 51:         cout << d5[i] << " ";
 52:     cout << endl;
 53:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

 55:     //Create a new deque as a copy of an existing deque
 56:     deque<double> d6(d3); //or deque<double> d6 = d3;
 57:     cout << "\nContents of d6 same as those of d3: ";
 58:     for (deque<double>::size_type i=0; i<d6.size(); i++)
 59:         cout << d6[i] << " ";
 60:     cout << endl;
 61:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
 62: }