From the course: C++ Templates and the STL

Input iterators

- [Instructor] An input iterator is the simplest form of iterator. It may be used to read values of an object once and then increment. Here I have a working copy of input-interator.cpp from Chapter Three of the exercise files. An input iterator is the simplest and most limited form of iterator. It's capable of reading but not writing, incrementing but not decrementing, and once a value is read it cannot be read again. The iostream object uses an input iterator for cin. And because of the unique nature of the cin object, again, all it does is read from a stream. The input iterator is defined separately. So here we declare it with the istream_iterator type and the type that it points to. And the default constructor, defaults to end of stream. So this is used for testing if you get to the end of the stream. And the standard in iterator is declared by passing cin as its parameter. So we use the input iterator like this. We test for end of stream, and if we don't have end of stream, we read the value and increment. And for our second value, we're not going to increment, and we're just going to read two values from cin, from standard in, and their numeric values because we've declared our iterator as having a double type. And we'll just multiply the two values together. So when I build and run, I can enter two numeric values. And, when I press Enter to submit, it grabs those two values and multiplies them together and it gives me the result. So this is what the input iterator does. Any additional functionality would make no sense for an input stream. It's very simple, and very limited, and it works well for its purpose.

Contents