Source of stream_iterators.cpp


  1: //stream_iterators.cpp
  2: //Illustrates STL input and output stream iterators.

  4: #include <iostream>
  5: #include <fstream>
  6: #include <vector>
  7: #include <iterator>
  8: #include <algorithm>
  9: #include <cstdlib>
 10: #include <cmath>
 11: using namespace std;

 13: int main()
 14: {
 15:     cout << "\nThis program illustrates the use of input and output "
 16:         "stream iterators.\nFirst we look at output stream iterators.";
 17:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 19:     int odds[]  = { 1, 3, 5, 7, 9  };
 20:     int evens[] = { 2, 4, 6, 8, 10 };
 21:     vector<int> v(5);

 23:     cout << "\nWe begin by displaying the contents of an array of "
 24:         "odd integers using the STL\ncopy algorithm and "
 25:         "and an \"anonymous\" output stream iterator:\n";
 26:     copy (odds, odds+5, ostream_iterator<int>(cout, " "));
 27:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 29:     cout << "\nNext we copy the values from an array of even integers "
 30:         "into a pre-sized vector\nof integers and then display the "
 31:         "contents of the vector in the same way:\n";
 32:     copy(evens, evens+5, v.begin());
 33:     copy (v.begin(), v.end(), ostream_iterator<int>(cout, " "));
 34:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 36:     cout << "\nNow we display the same vector contents again, this "
 37:         "time using a named\noutput stream iterator which also "
 38:         "uses a different value separator:\n";
 39:     ostream_iterator<int> outStream(cout, " | ");
 40:     copy (v.begin(), v.end(), outStream);
 41:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 43:     cout << "\nFinally, we copy odd integers from an array and "
 44:         "even integers from a vector\nto an output file and then "
 45:         "display the file to confirm. The odd integers are\noutput "
 46:         "one per line.";
 47:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 48:     ofstream outFile("stream_iterators.dat");
 49:     copy(odds, odds+5, ostream_iterator<int>(outFile, "\n"));
 50:     copy(v.begin(), v.end(), ostream_iterator<int>(outFile, " ! "));
 51:     outFile.close();  outFile.clear();
 52:     cout << "\nHere are the contents of stream_iterators.dat:\n";
 53:     system("type stream_iterators.dat | more"); //Portability alert!
 54:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 56:     cout << "\nNow we turn our attention to input stream iterators.";
 57:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 59:     cout << "\nFirst we read some integers entered from the keyboard, "
 60:         "using an anonymous\ninput stream iterator connected to the "
 61:         "standard input (the keyboard), and\ncopy those integers to a "
 62:         "vector, then display vector contents to confirm.";
 63:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');  
 64:     cout << "\nEnter 5 integers (terminate with end-of-file "
 65:         "on a new line): ";
 66:     copy(istream_iterator<int>(cin), istream_iterator<int>(), v.begin());
 67:     cin.clear();
 68:     cout << "\nHere are the vector contents: ";
 69:     copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
 70:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 72:     cout << "\nNext we read some more integers entered from the "
 73:         "keyboard, write their square\nroots directly to a file, "
 74:         "then display the file to confirm.";
 75:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 76:     cout << "\nEnter any number of integers on the line below and "
 77:         "\nterminate with an end-of-file on a new line:\n";
 78:     outFile.open("stream_iterators.dat");
 79:     transform(istream_iterator<int>(cin), istream_iterator<int>(),
 80:         ostream_iterator<double>(outFile, "\n"), sqrt);
 81:     outFile.close();  outFile.clear();
 82:     cin.clear();
 83:     cout << "\nHere are the contents of the file:\n";
 84:     system("type stream_iterators.dat | more"); //Portability alert!
 85:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 87:     cout << "\nNow we read the values from the previous output file, "
 88:         "and write the ceiling\nof each value to the standard output, "
 89:         "this time using named input and output\nstream iterators.";
 90:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 91:     ifstream inFile("stream_iterators.dat");
 92:     istream_iterator<double> inStream(inFile);
 93:     istream_iterator<double> inStreamEOF;
 94:     ostream_iterator<int> outputStream(cout, " ");
 95:     cout << "\nHere are the resulting values:\n";
 96:     transform(inStream, inStreamEOF, outputStream, ceil);
 97:                    //___^___
 98:                    //Same as using istream_iterator<double>() here.
 99:     inFile.close();  inFile.clear();
100:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

102:     cout << "\nFinally, we simply display the contents of the file "
103:         "stream_iterators.dat by\nincrementing an input stream "
104:         "iterator connected to that file.";
105:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
106:     inFile.open("stream_iterators.dat");
107:     istream_iterator<double> anotherInStream(inFile);
108:     istream_iterator<double> anotherInStreamEOF;
109:     cout << "\nHere are the values from the file:\n";
110:     while (anotherInStream != anotherInStreamEOF)
111:         cout << *anotherInStream++ << " ";
112:     inFile.close();  inFile.clear();
113:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
114: }