Of stream manipulators, stringstreams and character sets

A

Andy

1) Are the manipulators like std::endl and std::ends independent of
character set. That is, can I use the same manipulators in both the
following cases:

a)
<code>
std::basic_stringstream<char> strm;

strm<<"What goes up ... !"<<std::endl<<"You know the
rest!"<<std::ends;
</code>

b)
<code>
std::basic_stringstream<wchar_t> wstrm;

wstrm<<L"What goes up ... !"<<std::endl<<L"You know the
rest!"<<std::ends;
</code>

(Please overlook any windows specifics, see the problem below and
don't redirect me to ms.microsoft....)

2) Is it possible to clear a stringstream after writing to it and then
using it again? I mean I want this:

<code>

std::basic_stringstream<char> strm;

strm<<"What goes up ... !"<<std::endl<<"You know the
rest!"<<std::ends;

/*
... do something so that strm has no characters and we can write to
it (may
be remove std::ends if it prevents that)
*/

strm<<"Here is a new string ... with no strings attached before
it"<<std::endl;

</code>
 
T

tom_usenet

1) Are the manipulators like std::endl and std::ends independent of
character set. That is, can I use the same manipulators in both the
following cases:

a)
<code>
std::basic_stringstream<char> strm;

strm<<"What goes up ... !"<<std::endl<<"You know the
rest!"<<std::ends;
</code>

b)
<code>
std::basic_stringstream<wchar_t> wstrm;

wstrm<<L"What goes up ... !"<<std::endl<<L"You know the
rest!"<<std::ends;
</code>

(Please overlook any windows specifics, see the problem below and
don't redirect me to ms.microsoft....)

Yes, endl is templated on character type and traits type, so the above
is fine.
2) Is it possible to clear a stringstream after writing to it and then
using it again? I mean I want this:

<code>

std::basic_stringstream<char> strm;

strm<<"What goes up ... !"<<std::endl<<"You know the
rest!"<<std::ends;

There's no good reason to write std::ends to a stringstream. Doing so
just adds a \0 character, and is only useful for null terminated
strings, not for std::string. Perhaps you are confusing stringstream
with strstream?
/*
... do something so that strm has no characters and we can write to
it (may
be remove std::ends if it prevents that)
*/

strm.str(std::string()); //clear held string
strm.clear(); //clear error flags if any

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top