In the previous article of this series, titled "File Handling and Streams in C++," you saw how to handle files in an easy and convenient way. Getting input data from a file or putting such data into a file should be familiar to you now. In this article, we'll take a look at some more advanced tasks and tricks so you can gain speed and efficiency in your work.
Advanced File Handling with Streams in C++ - Reading Inside a Container (Page 3 of 4 )
You can use the same trick in the other direction (input), since it's just as easy. Let us suppose you have the input waiting for input in a predefined way. For example, we will try with a sequence of int types. Now we will copy in the other direction, so we need to indicate from where to copy, and to where. The first two members are the istream_iterator that will delimit the borders. When we pass a class to it, it will return an iterator to its start, and no parameter will just point to the end of file.
The last argument is a little more interesting. Under normal conditions, whenever you make a copy, you make sure that the target is large enough to hold the incoming stream of data. This is achieved by calling the destination resize() function, but in the case of this iterator type, this process is simplified by calling the push_back() method on the destination iterator. Hence, a vector can handle any size as long as you have enough physical and virtual memory available.
using namespace std;
return 0; // terminate program, nothing more to do
The rule stands. If you have defined a special class, and if you define the extraction function (operator >>) for it, you will be able to call it for this kind of input. For each member, the extraction function will be called to resolve the input. Naturally, you need to call the iterator creations to those kinds of template arguments.