Is there any direct way to calculate size of internal string in stringstream?
Here, str()
returns a copy and then it gets the size of string.
std::stringstream oss("String"); oss.str().size();
std::stringstream oss("Foo"); oss. seekg(0, ios::end); int size = oss. tellg(); Now, size will contain the size (in bytes) of the string.
A stringstream is an iostream object that uses a std::string as a backing store. An ostringstream writes to a std::string . An istringstream reads from a std::string . You read & write from & to an istringstream or ostringstream using << and >> , just like any other iostream object.
strstream has been deprecated since C++98, std::stringstream and boost::iostreams::array are the recommended replacements.
string stream is slow. Quite very slow. If you are writing anything performance critical that acts on large data sets ( say loading assets after a level change during a game ) do not use string streams.
There is:
std::stringstream oss("Foo"); oss.seekg(0, ios::end); int size = oss.tellg();
Now, size will contain the size (in bytes) of the string.
EDIT:
This is also a good idea to put after the above snippet as it puts the internal pointer back to the beginning of the string.
oss.seekg(0, ios::beg);
std::stringstream oss("String"); oss.seekp(0, ios::end); stringstream::pos_type offset = oss.tellp();
This is for the write pointer, but the result is the same for read pointer on Visual C++ v10.
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