Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of stringstream [duplicate]

Tags:

c++

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(); 
like image 945
user963241 Avatar asked Dec 13 '10 19:12

user963241


People also ask

How do I get the length of a Stringstream in C++?

std::stringstream oss("Foo"); oss. seekg(0, ios::end); int size = oss. tellg(); Now, size will contain the size (in bytes) of the string.

What is the difference between Stringstream and Istringstream?

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.

Is Stringstream deprecated?

strstream has been deprecated since C++98, std::stringstream and boost::iostreams::array are the recommended replacements.

Is Stringstream slow C++?

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.


2 Answers

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); 
like image 143
mishmash Avatar answered Sep 29 '22 03:09

mishmash


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.

like image 22
Steve Townsend Avatar answered Sep 29 '22 05:09

Steve Townsend