Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library User's Guide

12.1 The string Abstraction

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

In the C++ 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 C++ Standard Library not only provides facilities for manipulating sequences of 8-bit characters, but also for manipulating other types of character-like sequences, such as 16-bit wide characters. The datatypes string and wstring (for wide string) are simply typedefs of basic_string, defined as follows:

NOTE -- In the remainder of this chapter, we refer to the string datatype, but all the operations we introduce are equally applicable to wide strings.

As we have already noted, a string is similar in many ways to a vector of characters. Like the vector datatype, a string is associated with two sizes. 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 in a string without reallocation of a new internal buffer.

As in the vector datatype, 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.

12.1.1 Include Files

Programs that use strings must include the string header file:

Previous fileTop of DocumentContentsIndex pageNext file