C++: The String Container - dummies

By Stephen R. Davis

The most common form of a C++ array is the null-terminated character string used to display text, which clearly shows both the advantages and disadvantages of the array. Consider how easy the following appears:

cout << "This is a string";

But things go sour quickly when you try to perform an operation even as simple as concatenating two of these null-terminated strings:

char* concatCharString(const char* s1, const char* s2)
    int length = strlen(s1) + strlen(s2) + 1;
    char* s = new char[length];
    strcpy(s, s1);
    strcat(s, s2);
    return s;

The STL provides a string container to handle display strings. The string class provides a number of operations (including overloaded operators) to simplify the manipulation of character strings. The same concat() operation can be performed as follows using string objects:

string concat(const string& s1, const string& s2)
    return s1 + s2;

At its core, a string is still an array. The operations provided by the STL make it easier to manipulate string objects but not that much faster. Inserting into the middle of a string still involves moving the contents of arrays around.

The string class is actually an instantiation of the class template basic_class<T> with T set to char. The wstring class is another name for basic_class<wchar_t>. This class provides the same character manipulations shown here for wide strings.

The C++ ’11 definition adds u16string and u32string, which extends the string manipulation methods to UTF-16 and UTF-32 character strings. All comparisons between two string objects are performed lexicographically — that is, which of the two strings would appear first in the dictionary of the current language.