ostringstream
is backed by a stringbuf
, which inherits from streambuf
; when writing to an ostringstream
you are writing to the controlled output sequence of the streambuf
. According to 27.6.2:
2 - Each sequence is characterized by three pointers [...]
- the beginning pointer, or lowest element address in the array [...]
- the next pointer, or next element address that is a current candidate for reading or writing [...]
- the end pointer, or first element address beyond the end of the array [...]
So an ostringstream
effectively has constant time access to the current end of its output sequence.
Note that using strlen
on the output sequence would not work, anyway, as C++ strings are allowed to contain embedded nul characters.
Also note that direct string concatenation (string::operator+=(const string &)
) is likely to be more efficient than ostream
operators, as there is no requirement to go via virtual methods.