Source of merge1a.cpp


  1: //merge1a.cpp

  3: #include <iostream>
  4: #include <vector>
  5: #include <algorithm>
  6: using namespace std;

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the use of the STL "
 11:         "merge() algorithm (default version)\nto merge two sorted ranges "
 12:         "of integer values from two different vectors of\nintegers into a "
 13:         "single sorted range of values in another vector of integers.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 16:     int a1[] = {1, 2, 3, 5, 6, 9};
 17:     vector<int> v1(a1, a1+6);
 18:     cout << "\nHere are the contents of v1:\n";
 19:     for (vector<int>::size_type i=0; i<v1.size(); i++)
 20:         cout << v1.at(i) << " ";
 21:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 23:     int a2[] = {2, 4, 6, 8, 9, 10, 15};
 24:     vector<int> v2(a2, a2+7);
 25:     cout << "\nHere are the contents of v2:\n";
 26:     for (vector<int>::size_type i=0; i<v2.size(); i++)
 27:         cout << v2.at(i) << " ";
 28:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 30:     vector<int> v3(15);
 31:     cout << "\nHere are the contents of v3:\n";
 32:     for (vector<int>::size_type i=0; i<v3.size(); i++)
 33:         cout << v3.at(i) << " ";
 34:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 36:     cout << "\nNow merge the contents of v1 and v2 into v3.";
 37:     merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
 38:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 40:     cout << "\nHere are the revised contents of v3:\n";
 41:     for (vector<int>::size_type i=0; i<v3.size(); i++)
 42:         cout << v3.at(i) << " ";
 43:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 44: }