Thread: c++ strings

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    c++ strings

    Hello..

    I've been programming for some time, but I have never been using c++ strings before.. Im not sure when should I use them and how? Always when I have to deal with char *? Im a little confused since im used to char *sth = new char[BUF_SIZE]; ..

    Thanks for help

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    C++ strings:
    std::string mystring;

    Use a C++ string whenever you need store a string. As for how, google "C++ string tutorial". They're much easier to use than char*.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    So from now on I should do:

    Code:
    template <class t>
    class datalist {
    public:
    	datalist() { }
    	string name;
            t data;
    };
    instead of char *name.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    you'll need to #include <string> also. Be sure to do a search for string, so you can find all the functions available for it.

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    See this reference on string functions:

    http://www.cppreference.com/cppstring/index.html

    C++ strings are to C strings as C++ vectors are to C arrays - pretty much better in every
    way.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> C++ strings are to C strings as C++ vectors are to C arrays - pretty much better in every
    way.

    Yes indeed. All functions are associated with the strings . thing (sorry for my poorly chosen vocab), so you don't have to remember (taking examples from C), strcat, strlen etc. The + and = operators are overloaded, and there is a .size(), .find(), .c_str(), and lots of other functions with them, which IMO, makes them great.

    fast example C++ ::

    Code:
    std::string MyStringA = "this is a std::string. ";
    std::string MyStringB = "See how it Roars";
    std::string MyStringAB = MyStringA + MyStringB;
    
    std::cout<< '\n' << MyScringAB;// should be - this is a std::string. See how it Roars
    std::cout<< '\n' << MyScringAB.size(); // the size of the string, I'm not counting it :)
    obviously, if you REALLY wanted to, you could put a using namespace std;, or using std::cout; using std::string; or whatever to get rid of all those st::'s, but it's generally advised to use std::'s for large projects.

    Fast example, C: (If I can remember syntaxi etc)

    Code:
    char *MyStringA = "this is a std::string. ";
    char *MyStringB = "See how it Roars";
    char MyStringAB[100];
    
    strcpy( strcat( MyStringA, MyStringB ), MyStringAB ); 
    // can't remember whether the destination comes first or 
    // last with strcpy, nor can I remember whether they shuld be
    // char []'s, or char *'s or whether it makes a difference. 
    
    printf( "%s", MyStringAB );
    printf( "%d", strlen( MyStringAB );
    IMO, C++ way = nicer
    Last edited by twomers; 09-03-2006 at 12:56 PM.

  7. #7
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Quote Originally Posted by l2u
    Im not sure when should I use them and how?
    Always use them! C++ strings are a replacement for C strings. They're safer, easier, and can do more with less code. You can even get a C string from a C++ string with the c_str member function for any old libraries that use C strings.
    Quote Originally Posted by l2u
    Im a little confused since im used to char *sth = new char[BUF_SIZE]; ..
    It's even easier! You don't need a size because C++ strings grow automatically. You also don't need to remember to free memory because C++ strings manage memory automatically. I think those are two big problems in programming.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    As a clarification, c_str() gives a const C-style string. It's fine for any function call that reads a string, but function calls which modify or create a string will still need C-style strings.

    So the only time to use C-style strings are when you're using some function (e.g. an API call) that changes the contents of a buffer. As soon as the function call ends I make a string with the contents of the buffer and clean up the buffer.

    So, for example:

    Code:
    char * buffer = new char[neededSize];
    int bytesWritten = FillBufferFunction(buffer, neededSize); // Just some random function which fills the buffer
    std::string str(buffer, bytesWritten);
    delete[] buffer;
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Even then, it is generally better to use a vector, which deletes itself.
    Code:
    std::vector<char> buffer(neededSize);
    int bytesWritten = FillBufferFunction(&buffer[0], neededSize); // Just some random function which fills the buffer
    std::string str(buffer.begin(), buffer.begin() + bytesWritten);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM