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

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.

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism


Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger


show all menu

C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: