Sign in to follow this  

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
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-string
name += family; // string
name += '\n'; // character

cout << 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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by icecubeflower
+='/n' is like saying +=10 I think. We'll see.


Maybe you'd be surprised that += 10 actually does work.
int main() {
std::string s;
s += "me";
s += 104; // Assume the implementation is using ASCII (or compatible) character set.
std::cout << s << '\n';
}
Gives:
meh

Share this post


Link to post
Share on other sites
Quote:
Original post by icecubeflower
That's neat. Yeah I guess 104 and 'h' are the exact same thing to a compiler.


Not really, their types are different. The former is an int, while the latter is a char. However, there is an implicit conversion from int to char and as there is no std::string::operator += (int) overload, the implementation converts the types and uses the std::string::operator += (char) overload.

Share this post


Link to post
Share on other sites
[edit - bit slow, sorry Oxyd]

Quote:
Original post by icecubeflower
That's neat. Yeah I guess 104 and 'h' are the exact same thing to a compiler.


Not really. Not to a C++ compiler. 104 is a constant of type int. 'h' is a constant of type char.

What happens in Oxyd's example is that the compiler finds that std::string has an opertator+=(char), and because it can implicitly convert an int to a char, it considers this the best matching overload and does the conversion.

It generates a warning on VS about truncating a constant.

If you really needed to do this, you can suppress the warning and state your intention in the code more clearly by:


void f()
{
std::string s;
s+=char(104);
}

Share this post


Link to post
Share on other sites
One more question. How much memory do strings take up? I mean if I declare char[20] then it takes up 20 bytes. What if you have a string with 20 characters? What if you add one more character? What if you subtract some characters? Does it always allocate exactly the number of bytes it needs and does it always free the bytes it's not using or does it allocate a chunk of bytes at a time?

And what if you have a string with 20 characters and then you do this:

stringname[3]='\0';

Does the string automatically become 4 bytes or is the rest of the 20 bytes still sitting there and taking up memory for no reason?

I guess it depends on how that = operator is overloaded.

Share this post


Link to post
Share on other sites
Actually I just tried that, it doesn't work. If you stick a NULL in the middle of a string then instead of the string ending at the NULL like it would with a char array the NULL element is thrown out and the part after the NULL is concatenated onto the part that came before the NULL.

I don't know that I'll be able to use strings after all, I might have to re-write too much stuff.

Share this post


Link to post
Share on other sites
std::string is perfectly fine with storing null characters within the string. If your test suggest otherwise, your test is wrong.

As for memory, it is implementation defined. All you know for sure is that the memory allocated is at least as much as needed to store the string.

Share this post


Link to post
Share on other sites
Quote:
Original post by icecubeflower
One more question. How much memory do strings take up? I mean if I declare char[20] then it takes up 20 bytes. What if you have a string with 20 characters? What if you add one more character? What if you subtract some characters? Does it always allocate exactly the number of bytes it needs and does it always free the bytes it's not using or does it allocate a chunk of bytes at a time?


Not sure. But any time that you spend worrying about whether a 20 character string uses 20 bytes or 40 bytes, is almost definitely time wasted.

Quote:
And what if you have a string with 20 characters and then you do this:

stringname[3]='\0';


That's a silly way of doing things. If you want to resize the string to 4 characters, just call string::resize. It makes more sense.

Share this post


Link to post
Share on other sites
Quote:
I don't know that I'll be able to use strings after all, I might have to re-write too much stuff.
I recommend that you back up your current code, then just buckle down and do the re-write; your code will be better for it (and you'll probably save time in the long run).
Quote:
One more question. How much memory do strings take up? I mean if I declare char[20] then it takes up 20 bytes. What if you have a string with 20 characters? What if you add one more character? What if you subtract some characters? Does it always allocate exactly the number of bytes it needs and does it always free the bytes it's not using or does it allocate a chunk of bytes at a time?
How much memory the string uses is implementation-specific (AFAIK at least). Some implementations use COW; others use a fixed-size buffer for small strings. The long and short of it though is, don't worry about it unless you have specific reason to (such as needing to store millions of strings, or having limited memory available).
Quote:
And what if you have a string with 20 characters and then you do this:

stringname[3]='\0';

Does the string automatically become 4 bytes or is the rest of the 20 bytes still sitting there and taking up memory for no reason?

I guess it depends on how that = operator is overloaded.
Any element of a string objects can be '0', and setting an element to '0' will not erase or remove the following elements.

If you need to terminate a string or otherwise change its length, there are other (easier and safer) ways to do it than setting an element to null.

I strongly recommend that you stick with std::string, and just post back here (or perhaps in For Beginners) if you're having trouble figuring out how to revise your code.

Share this post


Link to post
Share on other sites

using namespace std;

#include <iostream>
#include <string>

int main()
{
string fun;

fun="blah";
fun[1]='\0';

cout<<"fun="<<fun<<"\n";

return 0;
}



I guess I'll try a different way. Probably there's functions or overloaded operators I can use or something. If I use the above code the output is
fun=bah
If it was a char array the output would have been
fun=b

Adios

Share this post


Link to post
Share on other sites
Quote:
Original post by icecubeflower
*** Source Snippet Removed ***

I guess I'll try a different way. Probably there's functions or overloaded operators I can use or something. If I use the above code the output is
fun=bah
If it was a char array the output would have been
fun=b

Adios


Uhh, don't do that. Use the resize() member function instead.

Share this post


Link to post
Share on other sites
Maybe because your console just doesn't print the null character at all, instead of trying to print an empty character or something else. If it doesn't advance the cursor a step, it looks like the string is shorter. If you check the actual content of the string you will see that fun[0] is 'b' and fun[2] is 'a', just as expected.

Share this post


Link to post
Share on other sites
you can use string::reserve( size_t ) to initaialise a buffer size to an anticipated final size but like others have said its generally not worth mucking around at this level and it may be the case (if doing lots of string appending) that better alternatives exist such as std::stringstream which provides good buffering behaviour as well as formatter operators at the cost of a larger initialisation time.

something that might be useful however and allow you to more easily migrate code is to use string::c_str() which will give you a null terminated c-style string with which to interface to c style functions taking const char * type arguments.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this