string
Class OperationsEven before the string class was added to C++, programmers needed to do things like assign strings. For C-style strings, they used functions from the C library for these tasks. The cstring
header file (formerly string.h
) supports these functions. For example, you can use the strcpy()
function to copy a string to a character array, and you can use the strcat()
function to append a string to a character array:
strcpy(charr1, charr2); // copy charr2 to charr1
strcat(charr1, charr2); // append contents of charr2 to char1
Listing 4.9 compares techniques used with string
objects with techniques used with character arrays.
// strtype3.cpp -- more string class features
#include <iostream>
#include <string> // make string class available
#include <cstring> // C-style string library
int main()
{
using namespace std;
char charr1[20];
char charr2[20] = "jaguar";
string str1;
string str2 = "panther";
// assignment for string objects and character arrays
str1 = str2; // copy str2 to str1
strcpy(charr1, charr2); // copy charr2 to charr1
// appending for string objects and character arrays
str1 += " paste"; // add paste to end of str1
strcat(charr1, " juice"); // add juice to end of charr1
// finding the length of a string object and a C-style string
int len1 = str1.size(); // obtain length of str1
int len2 = strlen(charr1); // obtain length of charr1
cout << "The string " << str1 << " contains "
<< len1 << " characters.
";
cout << "The string " << charr1 << " contains "
<< len2 << " characters.
";
return 0;
}
The string panther paste contains 13 characters.
The string jaguar juice contains 12 characters.
The syntax for working with string objects tends to be simpler than using the C string functions. This is especially true for more complex operations. For example, the C library equivalent of
str3 = str1 + str2;
is this:
strcpy(charr3, charr1);
strcat(charr3, charr2);
Furthermore, with arrays, there is always the danger of the destination array being too small to hold the information, as in this example:
char site[10] = "house";
strcat(site, " of pancakes"); // memory problem
The strcat()
function would attempt to copy all 12 characters into the site
array, thus overrunning adjacent memory. This might cause the program to abort, or the program might continue running but with corrupted data. The string
class, with its automatic resizing as necessary, avoids this sort of problem. The C library does provide cousins to strcat()
and strcpy()
, called strncat()
and strncpy()
, that work more safely by taking a third argument to indicate the maximum allowed size of the target array, but using them adds another layer of complexity in writing programs.
Notice the different syntax used to obtain the number of characters in a string:
int len1 = str1.size(); // obtain length of str1
int len2 = strlen(charr1); // obtain length of charr1
The strlen()
function is a regular function that takes a C-style string as its argument and that returns the number of characters in the string. The size()
function basically does the same thing, but the syntax for it is different. Instead of appearing as a function argument, str1
precedes the function name and is connected to it with a dot. As you saw with the put()
method in Chapter 3, this syntax indicates that str1
is an object and that size()
is a class method. A method is a function that can be invoked only by an object belonging to the same class as the method. In this particular case, str1
is a string
object, and size()
is a string
method. In short, the C functions use a function argument to identify which string to use, and the C++ string
class objects use the object name and the dot operator to indicate which string to use.
18.227.134.154