strings

Started by
43 comments, last by Kazade 15 years, 9 months ago
Don't make fun of me for this. I've been working on a 2D zelda like game for awhile. Suppose I've been using char[] arrays to store strings in and writing my own little functions to deal with them. Now say, I want to get with the times and use strings. Does c++ have a standard string library? I mean one that's going to compile on any machine? What library should I include? I vaguely remember something called a CString but also I think there was a competing string library. And then there's the STD Template library or whatever it's called. I think I'm going to start by reading about CStrings but if I'm barking up the wrong tree and someone wants to point me in the correct direction I'd appreciate it.
Advertisement
std::string. To use it, you need to #include <string>.
Quote:Original post by icecubeflower

And then there's the STD Template library or whatever it's called.


STL has (been) evolved into C++ standard library. For besic purposes, they are equivalent (although they are not, and some will get very upset over such claims).

CString, unless you intend to do WinAPI programming, has little use.



Relatively off-topic, but one of first page google links for "CString" links to an adult page....
Hey thanks. But the one thing I never understood about strings is how big are they? When I declare a char array I say char name[20] or whatever and make it 20 bytes. But look at this:

string name ("John");string family ("Smith");name += " K. ";         // c-stringname += family;         // stringname += '\n';           // charactercout << name;return 0;


So that will output "John K. Smith" and have an end line as the last character. How much stuff can you concatenate onto a string? Does it keep allocating more memory for itself?

The string resizes itself automatically when it needs to. BTW, you cannot use += with characters. It should be:

name += "\n"; Sorry, that's incorrect. Not sure where I got that from.
Quote:BTW, you cannot use += with characters. It should be:
Yes, you can. You can append a string, and a character as well. So if you want to append a character, use ' ', not " ".

Quote:How much stuff can you concatenate onto a string?
string::max_size() : Use max_size() on a string object to see how many characters it can have at maximum.
I cut and pasted that code directly from the link you gave me. I think you're right though. +='/n' is like saying +=10 I think. We'll see.
Quote:Original post by Gage64
BTW, you cannot use += with characters. It should be:

name += "\n";


Eh? VS's and Borland's std::string implementations allow for operator+=(char). Is this a non-standard extension or something?
Quote:Original post by icecubeflower
I think you're right though. +='/n' is like saying +=10 I think.


Not in C++. In C, a single character literal was of type int. In C++ single character literals are of type char, I imagine precisely to allow overloading to work correctly.
I edited my earlier reply. I apologize for any confusion.

This topic is closed to new replies.

Advertisement