Archived

This topic is now archived and is closed to further replies.

ByteMe95

how to clear a stringstream object in stl????

Recommended Posts

Ive looked to the vast reaches of the itnernet and from what I have seen there is practically absolutely no way to clear a stringstream object!!! How is that possible? Say i have a stringstream object A, and i use it in a loop. At the beginning of every loop I want to clear A so it doesnt accumulate when I extract the string wtih A.str() FRom what ive seen there is no way to clear the buffer, the only solution i have come up wtih is to declare A as a pointer and do A = new stringstream; before the use and delete A after the use is there no better way? ByteMe95::~ByteMe95() My S(h)ite

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
It just seems wrong that there''s no clear method.


doesn''t iostream::clear clears the stream io flags ?

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
quote:
Original post by ByteMe95
ss = "" produces compiler errors, i guess you cant do that with stringstreams



We''re talking about std::stringstream, not strstream, right ?


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
wow, ive never seen so many replies so fast! thanks everyone

There was only one correct answer to my question, and the answer is.... drumroll please.......

ss.str("")!!
thanks fruny

thanks again to everyone else for trying
- Rob

ByteMe95::~ByteMe95()
My S(h)ite

Share this post


Link to post
Share on other sites
ziphnor, i find it funny that you said .clear() worked for you when all logic dictates it shouldnt, so did you actually TRY it and it did in fact work, or were you just assuming it would?

ByteMe95::~ByteMe95()
My S(h)ite

Share this post


Link to post
Share on other sites
I find it''s usually best to use the .str("") and the .clear() methods together, as that should ensure a virtually fresh stringstream.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]

Share this post


Link to post
Share on other sites
quote:

ziphnor, i find it funny that you said .clear() worked for you when all logic dictates it shouldnt



Im not a complete idiot you know
In my code i use a stringstream in some string to value conversions, sometimes i had a problem with the stringstream containing some garbage after retrieving the value.
Using clear solved that problem, but when i think about it, this might have been due to some error during a conversion that was reset by clear....( i thought the garbage was leftover stuff from the previous strings added to the stringstream).

So the situation is not the same at all, i apologize.
Just tested it, and neither clear() or flush() empties the stringstream, but ss.str("") does....
Again, im sorry if i mislead anybody.

[edited by - ziphnor on June 11, 2002 2:19:18 PM]

Share this post


Link to post
Share on other sites