I am looking into std::wostringstream and how to properly use it. I was wondering if there is any way of knowing the length of the constructed string beforehand? I tried searching for information on MSDN and http://en.cppreference.com/w/ and finally google but couldn't find what I was looking for.
For example if I pass a reference of wostringstream to a function, is it possible the function to know if the stream is empty or if not and how long is it without doing a copy by calling .str()?
You could use the tellp() member function, which returns the output position indicator of the associated stream buffer. For instance:
#include <string>
#include <sstream>
#include <iostream>
int main()
{
std::wostringstream oss;
oss << L"Hello" << " World!" << std::endl;
std::cout << oss.tellp();
}
Here is a live example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With