Thread: char and <string>

  1. #1
    Registered User
    Join Date
    Nov 2003

    char and <string>

    I'm taught (from the book that i read) that there are two ways to work with strings. Either use an array of char s or use functions inside header file <string>. Although the book introduced the 2 together, it took way more pages convincing me that <string> is the better way to do it. Any ideas on why you would and you would NOT use <string>? Cuz I see many posts in this forum that still uses char arrays.
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    The best thing about the std::string class (from <string>) is that you do not have to worry about the memory allocation/deallocation with it. It handles that for you, and consequently, it is much more safe.

    There is less overhead with directly manipulating character arrays, than dealing with the string object itself, but this is usually of minimal importance, so std::string is the preferred way to go.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Sep 2003
    People worked with character arrays for many years, in C and in C++. Invariably this meant writing various functions to perform useful tasks with these arrays, in addition to those provided in <string.h>.

    In C++ it was common for people to write their own string classes, with this level of functionality included. The content of these classes would, of course, vary depending on who wrote them and what functionality they required.

    The C++ standard library has conveniently provided such a string class for you, so you don't need to write your own. The version that ships with your favourite compiler will have been written, tested and debugged, and by now used by thousands of programmers in probably millions of lines of code.

    Because it's part of the standard library, as defined by the C++ standard, you can rely on the std::string class being available on any (relatively) standard conforming compiler. Once you're familiar with it, you can change compilers, change OS, change employer, and still code the same. You can work with other people knowing that when they handle strings using the standard library you'll be able to understand their code.

    The std::string class doesn't do everything. If you need something that it doesn't provide, go ahead and write your own. Or use character arrays. But for most purposes, you'll probably find using a portable standard library class to be of benefit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #include <string> not working?
    By bulletbutter in forum C++ Programming
    Replies: 7
    Last Post: 04-26-2008, 12:46 AM
  2. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  3. vector <string> in a map
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2006, 10:34 AM
  4. Newbie Question: Can't use #include <string>
    By JohnnyCat in forum C++ Programming
    Replies: 7
    Last Post: 01-07-2005, 02:51 PM
  5. #include <string>
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 07-04-2002, 09:29 AM