Thread: string v.s. basic_string

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    string v.s. basic_string

    Hello everyone,


    I would like to learn some experiences about when should we use std::basic_string and when should we use std::string?

    I learned some Hello World level samples and now want to listen to your practical experiences. :-)


    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by SGI STL Web-page
    The types string and wstring are typedefs for, respectively, basic_string<char> and basic_string<wchar_t>.
    So, essentially, you are using "basic_string" when you are using string or wstring.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> when should we use std::basic_string and when should we use std::string?
    Always use string unless you know why you want to use basic_string.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Daved's answer is much better than mine!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I used basic_string a little while ago with a Windows app.
    I just made a typedef of basic_string<TCHAR> so it could be compiled as either ASCII or Unicode...

    Other than that, I don't know any other reason you'd need to use basic_string?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    I used basic_string a little while ago with a Windows app.
    I just made a typedef of basic_string<TCHAR> so it could be compiled as either ASCII or Unicode...

    Other than that, I don't know any other reason you'd need to use basic_string?
    Yes, that would be a good use-case.

    In fact, where I work, we have a similar concept where the "string" class is automatically unicode or ASCII/ANSI depending on a switch.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. string array v.s. int array
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2007, 12:07 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM