Top of document
©Copyright 1999 Rogue Wave Software

The string Abstraction

A string is basically an indexable sequence of characters. In fact, although a string is not declared as a subclass of vector, almost all of the vector operators discussed in Chapter 5 can be applied to string values. However, a string is also a much more abstract quantity, and, in addition to simple vector operators, the string data type provides a number of useful and powerful high level operations.

In the standard library, a string is actually a template class, named basic_string. The template argument represents the type of character that will be held by the string container. By defining strings in this fashion, the standard library not only provides facilities for manipulating sequences of normal 8-bit ASCII characters, but also for manipulating other types of character-like sequences, such as 16-bit wide characters. The data types string and wstring (for wide string) are simply typedefs of basic_string, defined as follows:

   typedef basic_string<char,strint_char_traits<char> > string;
    typedef basic_string<wchar_t> wstring;
 
Strings and Wide Strings

As we have already noted, a string is similar in many ways to a vector of characters. Like the vector data type, there are two sizes associated with a string. The first represents the number of characters currently being stored in the string. The second is the capacity, the maximum number of characters that can potentially be stored into a string without reallocation of a new internal buffer. As it is in the vector data type, the capacity of a string is a dynamic quantity. When string operations cause the number of characters being stored in a string value to exceed the capacity of the string, a new internal buffer is allocated and initialized with the string values, and the capacity of the string is increased. All this occurs behind the scenes, requiring no interaction with the programmer.

Include Files

Programs that use strings must include the string header file:

   # include <string>

Top of document