I have the following code, which has sin_start(cin) function. I am not sure if there is any such function defined in C++ or if I need to define it myself. But somehow this code compiles and runs too.
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
cout << "Enter integers: ";
istream_iterator<int> sin_start(cin);
}
The code compiles because it is valid code. It just doesn't do anything meaningful.
sin_startis not a function, it is a variable of typeistream_iterator<int>, whereistream_iteratoris a template class defined in the<iterator>header file. It is being passedstd::cinas a parameter to its constructor, so it knows which stream to read data from.But the code simply exits after that variable is constructed. It is not trying to read any data. For that, try something more like this:
That will read integers until the user cancels input on the terminal.
However, in comments, you mention that you want to read integers until the user presses Enter instead. In that case, you need a different approach, since
istream_iteratorusesoperator>>internally, which skips leading whitespace on each read, and Enter is treated as whitespace. Try something more like this instead: