|
|
c m s c 214
f a l l 2 0 0 1 |
© by Charles Lin. All rights reserved. You must receive written permission from Charles Lin to reproduce this webpage in any form.
C-style string predates C++ strings. From CMSC 114, you should know that a C-style string has a "char *" type, and the string ends in a null character. Unfortunately, because of this, C-style strings are harder to use than other simple types in C. In particular, you must use the following functions:
This solution is due to the fact that strings can be arbitrary in size, where int's, float's, bool's, and char's have fixed sizes.
Wouldn't it be much better if strings were a class, where assignment and == are correctly overloaded to do what you expect them to, and wouldn't it be nice if string objects could manage the memory needed.
That's what the developers of C++ standard library thought. Therefore, C++ has a string class.
The string class should be very easy to learn. Yet, students often make their lives more complicated by trying to treat C++ like strings as C strings. Don't do this!
Let's go through the basic operations of a C++ string.
#include <string> // need this for C++ strings
int main()
{
string str; // str contains empty string
string str2 = "cat"; // str2 contains cat
string str3 = str2; // str3 contains cat
}
Before you can use the C++ strings, you must include the
library. Notice that you include <string> instead
of <string.h>.
Be careful about making any assumptions on C++ strings. In particular, you should not assume that such strings end in a null character.
Look at the declaration above. Notice that "str2" is assigned
to "cat". "cat" is a C-style string, not a C++-style string. So,
obviously, there is a constructor in the string class that looks like:
string( const char *other ); // Constructor for string
Of course, you can copy one C++ string object to another (see
"str3" above).
string s; cin >> s; // Stores string from standard inputNotice that you don't have to worry about allocating memory. The string class automatically handles this for you. The semantics of reading into a string should be:
string s1 = "cat", s2 = "cat";
if ( s1 == s2 )
cout << "same" << endl;
This performs a case-sensitive comparison of the two string
objects. Recall why this doesn't work for C-style strings.
(Answer: C-style strings use char * pointers, so using == compares
pointer addresses, not the string content).
string s1 = "cat"; cout << s1 << endl;
string s1 = "cat"; // Both print 3, the length of "cat" cout << s1.length() << endl; cout << s1.size() << endl;
string s1 = "cat"; // Print 'a' cout << s1[ 1 ] << endl;
string s1 = "cat", s2 = "house", s3; s3 = s1 + s2; // Print 'cathouse' cout << s3 << endl;You don't use "strcat" to concatenate using C++-style strings.
Just because C++-style strings are easier to use than C-style strings, don't believe that you can simply forget C-style strings. Many functions in C primarily rely on C-style strings, especially those dealing with network applications. Also, such basic things as "argc" are still C-style strings. Unfortunately, you can't exactly wipe out C-style strings. However, when you don't need to use them, it's better and easier to use C++-style strings.
There is one drawback to C++-style strings. They are templates. Because of this, they can produce horrible looking error messages. Hopefully, compilers will be smart enough one day to avoid having such verbose error messages, but until then, you have to deal with it.