Thread: TString manipulation...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    2

    TString manipulation...

    Greetings,

    This must seem like a really basic question, but how would
    one add ints, doubles, etc... to a TString?

    For example say I have:


    Code:
    int server = 8888;
    double top = 38.3534535361;
    
    tstring serverTString = "Server: "
    tstring topTString = "Top: "
    How could I end up with the following:

    Code:
    tmp = "Server:8888, Top: 38.3534535361";
    I am new to this, but I suspect I would have
    to use something like swprintf?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I'm assuming that a tstring is simply a basic_string<TCHAR>.

    If that's the case, then you should wrap all your string literals using the _T() or _TEXT macro. Otherwise, if you never plan on compiling you code for UNICODE target then don't bother with tstring, just use string.

    That aside, printf() style formatting is done in C++ using streams, like cin, cout, fstream's etc...

    basic_string<> can have I/O streams as well:
    * basic_istringstream<>
    * basic_ostringstream<>
    * basic_stringstream<>

    These have cooresponding typedef's just like basic_string<> does:
    * stringstream (for type char)
    * wstringstream (for type wchar_t)
    * tstringstream (don't know if this is typedef'd for you or not)

    And there are input only and output only versions of each of these like istringstream and ostringstream.

    Here is some code that defines the tstring and tostringstream types and uses them.
    Code:
    //#define _UNICODE // enable to test as a UNICODE build
    #include <tchar.h>
    #include <sstream>
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    // typedef a TCHAR string and ostringstream types
    typedef basic_string<TCHAR, 
                         char_traits<TCHAR>, 
                         allocator<TCHAR> > tstring;
    
    typedef basic_ostringstream<TCHAR, 
                                char_traits<TCHAR>, 
                                allocator<TCHAR> > tostringstream;
    
    // define "tcout" to be either cout or wcout depending 
    // on the true type of TCHAR
    #ifdef _UNICODE
    #define tcout wcout
    #else
    #define tcout cout
    #endif
    
    int main()
    {
        tostringstream ts_out;
    
        ts_out << _T("double val = ") << 1234.5678 << endl
               << _T("long val = ") << 69L << endl
               << _T("long val in hex = 0x") << hex << 105L << dec << endl;
    
        tstring results = ts_out.str();
    
        tcout << results;
    
        return 0;
    }//main
    Your alternative is to use _stprintf() on a TCHAR buffer and assign that to your tstring.

    gg

  3. #3
    Code:
    // typedef a TCHAR string and ostringstream types
    typedef basic_string<TCHAR, 
                         char_traits<TCHAR>, 
                         allocator<TCHAR> > tstring;
    
    typedef basic_ostringstream<TCHAR, 
                                char_traits<TCHAR>, 
                                allocator<TCHAR> > tostringstream;
    Ok, now everytime I see something like this I run and hide in the corner.. It doesn't make much sense to me. I gather it's like using a meta language?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Templates are fun!!

    You should pull out your favorite C++ book and read up on em. Just think of the stuff in between the <> as a list of parameters, where the parameter is the type itself (not an instance of that type).
    Here's some code to demonstrate the basics.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    // This class can represent an array of 100 elements of any type.
    template <typename T>
    class Array
    {
        T array[100];
    public:
        T& operator[](size_t index) {return array[index];}
    };//Array
    
    typedef Array<double> DoubleArray;
    
    int main()
    {
        // Create an instance of the Array class where T == int
        Array<int> iArray;
    
        // Create an instance of Array of doubles using the typdef name
        DoubleArray dArray;
    
        // do something with em
    
        return 0;
    }//main
    gg

  5. #5
    Cool. Very useful!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. message box LPCWSTR manipulation and keyword question
    By stanlvw in forum Windows Programming
    Replies: 11
    Last Post: 05-27-2008, 12:36 PM
  2. Some help with string manipulation
    By InvertedSaint in forum C Programming
    Replies: 2
    Last Post: 08-09-2006, 06:32 AM
  3. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM
  4. File manipulation
    By Shadow in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 08:07 AM
  5. Tstring 's and their data types
    By tegwin in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:01 PM