C Formatting Using STL

This is a discussion on C Formatting Using STL within the C++ Programming forums, part of the General Programming Boards category; With wxWidgets, the wxString class is very similar to the STL string class, but the STL does not include a ...

  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,088
    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,672
    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 01:04 PM.
    I used to be an adventurer like you... then I took an arrow to the knee.

  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,088
    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, 05: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

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