Using string and stringstream effectively

This is a discussion on Using string and stringstream effectively within the C++ Programming forums, part of the General Programming Boards category; I've been scratching my head over this one for a bit. I've looked around and various references, articles, tutorials, etc. ...

  1. #1
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    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.
    - Leeor

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    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!
    - Leeor

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    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
    22,265
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 06:45 PM
  2. stringstream problem
    By black_spot1984 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 05: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, 03:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21