Chapter 4: The `string' data type

We're always interested in getting feedback. E-mail us if you like this guide, if you think that important material is omitted, if you encounter errors in the code examples or in the documentation, if you find any typos, or generally just if you feel like e-mailing. Send your email to Frank Brokken.

Please state the document version you're referring to, as found in the title (in this document: 4.4.2).

C++ offers a large number of facilities to implement solutions for common problems. Most of these facilities are part of the Standard Template Library or they are implemented as generic algorithms (see chapter 11).

Among the facilities C++ programmers have developed over and over again (as reflected in the Annotations) are those for manipulating chunks of text, commonly called strings. The C programming language offers rudimentary string support: the ascii-z terminated series of characters is the foundation on which a large amount of code has been built.

Standard C++ now offers a string type of its own. In order to use string-type objects, the header file string must be included in sources.

Actually, string objects are class type variables, and the class is introduced for the first time in chapter 5. However, in order to use a string, it is not necessary to know what a class is. In this section the operators that are available for strings and some other operations are discussed. The operations that can be performed on strings take the form

stringVariable.operation(argumentList)

For example, if string1 and string2 are variables of type string, then

string1.compare(string2)

can be used to compare both strings. A function like compare(), which is part of the string-class is called a member function. The string class offers a large number of these member functions, as well as extensions of some well-known operators, like the assignment (=) and the comparison operator (==). These operators and functions are discussed in the following sections.

4.1: Operations on strings

Some of the operations that can be performed on strings return indices within the strings. Whenever such an operation fails to find an appropriate index, the value string::npos is returned. This value is a (symbolic) value of type string::size_type, which is (for all practical purposes) an int.

Note that in all operations where string objects can be used as arguments, char const * values and variables can be used as well.

Some string-member functions use iterators. Iterators will be covered in section 11.1. The member functions that use iterators are listed in the next section (4.2), they are not further illustrated below.

The following operations can be performed on strings:

4.2: Overview of operations on strings

In this section the available operations on strings are summarized. There are four subparts here: the string-initializers, the string-iterators, the string-operators and the string-member functions.

The member functions are ordered alphabetically by the name of the operation. Below, object is a string-object, and argument is either a string or a char const *, unless overloaded versions tailored to string and char const * parameters are explicitly mentioned. Object is used in cases where a string object is initialized or given a new value. Argument remains unchanged.

With member functions the types of the parameters are given in a function-prototypical way. With several member functions iterators are used. At this point in the Annotations it's a bit premature to discuss iterators, but for referential purposes they have to be mentioned nevertheless. So, a forward reference is used here: see section 11.1 for a more detailed discussion of iterators.

Finally, note that all string-member functions returning indices in object return the predefined constant string::npos if no suitable index could be found.

4.2.1: The string-initializers

4.2.2: The string-iterators

See section 11.1 for details about iterators.

4.2.3: The string-operators

4.2.4: The string member functions