transform algorithm with ostream_iterator : ostream_iterator « File Stream « C++ Tutorial






#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;

int sum(int val1, int val2) { return val2 + val1; }

int main()
{
  int array1[5] = {0, 1, 2, 3, 4};
  int array2[5] = {6, 7, 8, 9, 10};
  ostream_iterator<int> out(cout, " ");

  // Put sums of corresponding array1 and array2 elements into output stream:
  transform(&array1[0], &array1[5], &array2[0], out, sum);

  cout << endl;
  return 0;
}
6 8 10 12 14








12.12.ostream_iterator
12.12.1.Use ostream_iterator
12.12.2.Create ostream_iterator for writing int values to cout
12.12.3.Use ostream_iterator and copy to display collection content
12.12.4.transform algorithm with ostream_iterator