Demonstrate ostream_iterator with files - C++ STL Algorithm

C++ examples for STL Algorithm:ostream_iterator

Description

Demonstrate ostream_iterator with files

Demo Code

#include <fstream>
#include <algorithm>
#include <iterator>
#include <list>
using namespace std;
int main()//from  w w  w  .  j a v a 2 s.  c  o m
{
  int arr[] = { 11, 21, 31, 41, 51 };
  list<int> theList;
  for (int j = 0; j<5; j++)              //transfer array
    theList.push_back(arr[j]);     //   to list
  ofstream outfile("ITER.DAT");       //create file object
  ostream_iterator<int> ositer(outfile, " ");  //iterator
                         //write list to file
  copy(theList.begin(), theList.end(), ositer);
  return 0;
}

Related Tutorials