18.1. Introduction
The class template basic_string provides typical string-manipulation operations
such as copying, searching, etc. The template definition and all support
facilities are defined in namespace std;
these include the typedef statement
typedef basic_string< char > string;
that creates the alias type
string for basic_string< char
>. A typedef also is provided for
the wchar_t type. Type
wchar_t
stores characters (e.g., two-byte characters, four-byte characters, etc.) for
supporting other character sets. We use string
exclusively throughout this chapter. To use strings, include header
file <string>.
A string object can be
initialized with a constructor argument such as
string text( "Hello" ); // creates a string from a const char *
which creates a string containing the characters in
"Hello", or with two constructor arguments as in
string name( 8, 'x' ); // string of 8 'x' characters
which creates a string containing eight 'x'
characters. Class string also provides a
default constructor (which creates an empty string) and a copy constructor. An
empty string is a
string that does not contain any
characters.
A string also can be
initialized via the alternate constructor syntax in the definition of a
string as in
string month = "March"; // same as: string month( "March" );
Remember that operator = in the
preceding declaration is not an assignment; rather it is an implicit call to the
string class constructor, which does the
conversion.
Note that class string provides no conversions from
int or char to string in a string
definition. For example, the definitions
string error1 = 'c';
string error2( 'u' );
string error3 = 22;
string error4( 8 );
result in syntax errors. Note that assigning a
single character to a string object is permitted in
an assignment statement as in
Common Programming Error 18.1
|
Attempting to convert an int
or char to a string via an
initialization in a declaration or via a constructor argument is a compilation
error. |
Unlike C-style char * strings, strings are
not necessarily null terminated. [Note: The C++ standard document provides only a description of
the interface for class string—implementation
is platform dependent.] The length of a string can be retrieved with member function length and with member
function size. The subscript
operator, [], can be used with
strings to access and modify individual
characters. Like C-style strings, strings have a first subscript of
0 and a last subscript of length() – 1.
Most string member functions
take as arguments a starting subscript location and the number of characters on
which to operate.
The stream extraction operator (>>) is
overloaded to support strings. The statements
string stringObject;
cin >> stringObject;
declare a string object and read a string from the standard input device. Input is delimited by
whitespace characters. When a delimiter is encountered, the input operation is
terminated. Function getline also is
overloaded for strings. Assuming string1 is a string,
the statement
reads a string from the keyboard into
string1. Input is delimited by a newline
('\n'), so getLine can read a line of text into a
string object.