Thread: Using string and stringstream effectively

  1. #1
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147

    Using string and stringstream effectively

    I've been scratching my head over this one for a bit. I've looked around and various references, articles, tutorials, etc. but I can't seem to find an effective solution.

    I'm outputting a series of information using stringstreams to easily generate strings with number values. Simple enough. I can't seem to get the stringstream cleared so I can use it again for a new operation. This is the code that I have and I just know that there's a more effective way to handle this:

    Code:
    stringstream tmpStr, tmpStr2;
    tmpStr << "Some data";
    tmpStr2 << "Some data 2";
    
    drawText(mTestFont, tmpStr.str().c_str(), 0, 0);
    drawText(mTestFont, tmpStr2.str().c_str(), 0, 20);
    Declaring a new stringstream object every time I want a new bit of information can't possibly be an effective way of doing this and it looks truly ridiculous when I have 10 or 12 different strings I'm trying to output in the same function. Not to mention the maintenance headache.

    I've tried using the clear() and seekg(0, ios::beg) functions but haven't had any luck. I simply end up with the original string with the string tacked onto the end of it.

    What's the appropriate way to properly clear/flush a string stream so that I can use it again with a 'blank slate' so to speak?

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by leeor_net
    What's the appropriate way to properly clear/flush a string stream so that I can use it again with a 'blank slate' so to speak?
    I believe it should be:
    Code:
    tmpStr.str("");
    You may need to use the clear() member function as well to clear say, an EOF state if you had used the stringstream as an output stream.

    You might also want to consider the use of Boost.Format, depending on your requirements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    Thanks, laserlight. As always, a prompt, consise answer without lessons in 'coding ettiquette'. I figured it had to be something as simple as that.

    Thank you!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You might also consider boost::lexical_cast for conversions, along with Boost.Format. Handy stuff.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    You know I always seem to use strings instead, never had a need to use stringstream except in special cases, especially conversions. Perhaps if you keep having to reuse a stringstream might as well try using just strings?
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by execute
    You know I always seem to use strings instead, never had a need to use stringstream except in special cases, especially conversions. Perhaps if you keep having to reuse a stringstream might as well try using just strings?
    I agree: in the given example, the use of a string will suffice, and a stringstream is unnecessary complication. However, leeor_net mentioned "using stringstreams to easily generate strings with number values", and presumably the actual code is something like this:
    Code:
    for (int i = 0; i < n; ++i)
    {
        stringstream ss;
        ss << "Some data " << n;
        drawText(mTestFont, ss.str().c_str(), 0, 0);
    }
    A stringstream would actually be useful in such a case, as demonstrated.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stringstream problem
    By black_spot1984 in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2008, 05:45 PM
  2. stringstream problem
    By black_spot1984 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 04:09 PM
  3. Can someone help with my seg fault?
    By John_L in forum C++ Programming
    Replies: 23
    Last Post: 03-01-2008, 04:04 PM
  4. Replies: 3
    Last Post: 12-28-2006, 04:18 PM
  5. one more cin question
    By tinkerbell20 in forum C++ Programming
    Replies: 15
    Last Post: 06-10-2005, 02:27 PM