Source of partial_sort_copy1a.cpp


  1: //partial_sort_copy1a.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:         "partial_sort_copy() algorithm\n(default version) to "
 12:         "sort a range of values from a vector of integers of\nsize 12 "
 13:         "into ascending order and copy as many of them as will fit "
 14:         "into a\nrange within a second vector of integers. The contents "
 15:         "of the initial\nvector are unchanged by this action.";
 16:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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

 25:     int a2[] = {100, 200, 300, 400, 500};
 26:     vector<int> v2(a2, a2+5);
 27:     cout << "\nHere are the initial contents of vector v2:\n";
 28:     for (vector<int>::size_type i=0; i<v2.size(); i++)
 29:         cout << v2.at(i) << " ";
 30:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 32:     cout << "\nNow we make the following call:";
 33:     cout << "\np = partial_sort_copy(v1.begin(), v1.end(), "
 34:         "v2.begin(), v2.begin()+3);";
 35:     vector<int>::iterator p;
 36:     p = partial_sort_copy(v1.begin(), v1.end(), v2.begin(), v2.begin()+3);
 37:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 39:     cout << "\nAfter that call, here are the contents of v1:\n";
 40:     for (vector<int>::size_type i=0; i<v1.size(); i++)
 41:         cout << v1.at(i) << " ";
 42:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 44:     cout << "\nAnd here are the contents of v2:\n";
 45:     for (vector<int>::size_type i=0; i<v2.size(); i++)
 46:         cout << v2.at(i) << " ";
 47:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 49:     cout << "\nThe iterator p returned by the algorithm points to ";
 50:     if (p == v2.end())
 51:         cout << "\nthe end of the ouput container.";
 52:     else
 53:         cout << "the value " << *p << ".";
 54:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 56:     cout << "\nNext we make the following call:";
 57:     cout << "\np = partial_sort_copy(v1.begin()+2, v1.end()-2, "
 58:         "v2.begin(), v2.end());";
 59:     p = partial_sort_copy(v1.begin()+2, v1.end()-2, v2.begin(), v2.end());
 60:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 62:     cout << "\nAfter that call, here are the contents of v1:\n";
 63:     for (vector<int>::size_type i=0; i<v1.size(); i++)
 64:         cout << v1.at(i) << " ";
 65:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 67:     cout << "\nAnd here are the contents of v2:\n";
 68:     for (vector<int>::size_type i=0; i<v2.size(); i++)
 69:         cout << v2.at(i) << " ";
 70:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 72:     cout << "\nThe iterator p returned by the algorithm points to ";
 73:     if (p == v2.end())
 74:         cout << "\nthe end of the ouput container.";
 75:     else
 76:         cout << "the value " << *p << ".";
 77:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 78: }