std::vector<char> v( (std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>() );
std::vector<T>
has a constructor that takes two iterators on <T>
- one for the beginning and one for the end of the range.
This constructor makes an input stream iterator from an input stream in
:
std::istreambuf_iterator<char>(in)
You can access its elements going forward until you reach the end of the stream. Once you reach the end of stream, the iterator becomes equivalent to an iterator created using the default constructor:
std::istreambuf_iterator<char>()
Therefore, passing this pair of iterators constructs a vector<T>
from the data read from an input stream. The entire stream will be consumed.
std::string content((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
There is '\0' at the end of the string.