Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to know the length of std::wostringstream before calling .str()?

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()?

like image 394
Ivan Zlatanov Avatar asked Jan 25 '26 00:01

Ivan Zlatanov


1 Answers

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.

like image 190
Andy Prowl Avatar answered Jan 26 '26 15:01

Andy Prowl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!