Introduction
The C++ 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 [1] 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 .
[1] Type wchar_t commonly is used to represent Unicode®, which does have 16-bit characters, but the size of wchar_t is not fixed by the standard. The Unicode Standard outlines a specification to produce consistent encoding of the world's characters and symbols. To learn more about the Unicode Standard, visit www.unicode.org.
A string object can be initialized with a constructor argument such as
string text( "Hello" ); // creates string from 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 construction 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
string1 = 'n';
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 stringimplementation 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 statement
string stringObject; cin >> stringObject;
reads a string from the standard input device. Input is delimited by white-space characters. When a delimiter is encountered, the input operation is terminated. Function getline also is overloaded for strings. The statement
string string1; getline( cin, string1 );
reads a string from the keyboard into string1. Input is delimited by a newline (' '), so getLine can read a line of text into a string object.