Quote:
|
I'm just wondering how the program presented at the end of the tutorial knows when to stop accepting words from the standard input.
|
It reads till it reads EOS (End Of Stream) that beeing the end of file if it's a file you're reading from or a termination of your input stream when you read from stdin.
Quote:
|
It seems that I just don't know how to close the stream?
|
Terminate the tranmission, on *nix thats <ctrl>+D in DOS it's <ctrl>+S, it all depends on what system you're running.
A small example:
Code:
#include <iterator>
#include <vector>
#include <numeric>
#include <iostream>
using namespace std;
int main ()
{
vector<int> d;
int total = 0;
//
// Collect values from cin until end of file
// Note use of default constructor to get ending iterator
//
cout << "Enter a sequence of integers (eof to quit): " ;
copy(istream_iterator<int,char>(cin),
istream_iterator<int,char>(),
inserter(d,d.begin()));
//
// stream the whole vector and the sum to cout
//
copy(d.begin(),d.end()-1,
ostream_iterator<int,char>(cout," + "));
if (d.size())
cout << *(d.end()-1) << " = " <<
accumulate(d.begin(),d.end(),total) << endl;
return 0;
}
Since this reads anything of type
int then anything which isn't an int, like a char.