Thread: C Formatting Using STL

  1. #1
    Chad Johnson
    Join Date
    May 2004
    Posts
    154

    Question C Formatting Using STL

    With wxWidgets, the wxString class is very similar to the STL string class, but the STL does not include a Format function as does wxString.

    As you may know, you can say

    wxString str = wxString::Format ("%s %i", "whatever", 12345);

    and the variable str will be formatted like in C using sprintf

    sprintf (buffer, "%s %i", "whatever", 12345);

    Is there a Format() function in the STL for strings or something similar? Right now I use standard C strings rather than the STL because this very reason (I want to go to STL, but this is the only drawback). I know I could use osstringstream to get similar functionality, but I would rather use a C approach.

    Any ideas?

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I don't believe there is a standard format function for STL strings (just use stringstreams!), but you can try the Boost Format Library (just use stringstreams!). Otherwise I think you are stuck with sprintf and C style strings (just use stringstreams!).

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    wxString str = wxString::Format ("%s %i", "whatever", 12345);
    Like what jlou said... If you want to format a std::string in the same way as the above wxString you would need to use a stringstream container to handle the formating as an intermediate step.

    Code:
    #include <string>
    #include <sstream>
    ...
    std::string input("whatever");
    int i = 12345;
    std::stringstream format;
    std::string str;
    ...
    format << input << ' ' << i;  // Store/format data into stringstream
    str = format.str();  // str now equals "whatever 12345"
    Could also skip the use of variables above and just directly say:

    Code:
    format << "whatever" << ' ' << 12345;
    str = format.str();
    Last edited by hk_mp5kpdw; 11-18-2004 at 02:04 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    Are you SURE there is no other way using the STL?

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I am not positive, since I don't know every function in the STL, but I am 99% sure. You would have to use a third party library (like boost), write your own version (someone in my company did) or use stringstreams.

    The reason one is not available is because sprintf is not type-safe and is easy to make mistakes with that cannot be caught by the compiler. I believe they tried to avoid providing functionality like that in the C++ standard library.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  2. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  3. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM