I guess somebody had to ask these questions, so please bear with me.
Let’s consider the following beasts:
- ostream
- istream
- iostream
- streambuf
string– Following Benjamin’s observation, this one does not belong here.- stringbuf
and the following iterators:
- ostream_iterator
- ostreambuf_iterator
- istream_iterator
- istreambuf_iterator
Questions:
Why isn’t there any iostream_iterator, streambuf_iterator, (std::string::iterator and std::string::const_iterator do exist) or string_iteratorstringbuf_iterator?
Why don’t the 4 iterators listed above have a const version like std::vector<T>::const_iterator?
How are we supposed to iterate over any of those beasts if they don’t provide the begin() and end() methods? What is the purpose of the above 4 iterators?
N00b question: Where can I find a good reference that details the logic behind these classes and, also, some decent examples on how to use them?
Hopefully, someone with experience will provide a good answer for the above questions.
PS: Real life situation:
void writeToStream(std::ostream &out);
int main ()
{
std::ostringstream byteStream;
writeToStream(byteStream);
std::string byteString = byteStream.str();
for (unsigned short i = 0; i < byteString.size(); ++i)
{
//do something with each byteString[i]
}
return 0;
}
My guess is that I could have used ostream and iterate over its elements by using an iterator just like for std::vector, instead of using ostringstream and the str() method to convert it to a string, but I couldn’t find any information regarding this.
Don’t know. Probably because the semantics would be hard to define when doing both input and output. What do you expect to happen when you go forward and then backwards in a stream with an iterator?
These are easily covered by the existing iterators:
Because when you iterate across a stream the state of the stream is changing.
Because a stream is usually one direction. You don’t iterate from the beginning but rather iterate from the current position in the stream to the end. So you just declare an iterator specifying the stream. The iterator starts at the current position and moves towards the end. You can declare an end of stream iterator by just not spcifying the stream.
Here is a good place.
Do a search for “C++ istream_iterator” you will get a lot of hits.
Hopefully.