Size of stringstream [duplicate]

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

Solution 1:

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);

Solution 2:

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.