Source of copy_if1a.cpp


  1: //copy_if1a.cpp

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

 10: int main()
 11: {
 12:     cout << "\nThis program illustrates the use of the STL copy_if() "
 13:         "algorithm from\n<algorithm> to copy all odd integers "
 14:         "from a vector of integers to a\ndeque of integers.";
 15:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 17:     int a[] = {2, 5, 6, 7, 10, 11, 14};
 18:     vector<int> v(begin(a), end(a));
 19:     deque<int> d(4);
 20:     auto endOfCopiedRange =
 21:         copy_if(begin(v), end(v), begin(d), [](int n){return n%2==1;});
 22:     copy(begin(d), end(d), ostream_iterator<int>(cout, " "));
 23:     cout << "\nThe value at the end of the copied range is "
 24:         << *endOfCopiedRange << ".\n";
 25:     cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
 26: }