Previous Section
 < Free Open Study > 
Next Section


C.9 The Header File string

This header file supplies a programmer-defined data type (specifically, a class) named string. Associated with the string type are a data type, string::size_type, and a named constant, string::npos, defined as follows:

string::size_type

An unsigned integer type related to the number of characters in a string

string::npos

The maximum value of type string::size_type

There are dozens of functions associated with the string type. Below are several of the most important ones. In the descriptions, s is assumed to be a variable (an object) of type string.

s.c_str()

  • Arguments:

None

  • Function return value:

The base address of a C string (null-terminated char array) corresponding to the characters stored in s

s.find(arg)

  • Argument:

An expression of type string or char, or a C string (such as a literal string)

  • Function return value:

A value of type string:: size_type that gives the starting position in s where arg was found. If arg was not found, the return value is string::npos.

  • Note:

Positions of characters within a string are numbered starting at 0.

getline(inStream, s)

  • Arguments:

An input stream inStream (of type istream or ifstream) and a string object s

  • Effect:

Characters are input from inStream and stored into s until the new-line character is encountered. (The newline character is consumed but not stored into s.)

  • Function return value:

Although the function technically returns a value (which we do not discuss here), programmers usually invoke the function as though it were a void function.

s.length()

  • Arguments:

None

  • Function return value:

A value of type string:: size_type that gives the number of characters in the string

s.size()

  • Arguments:

None

  • Function return value:

The same as s.length()

s.substr(pos, len)

  • Arguments:

Two unsigned integers, pos and len, representing a position and a length. The value of pos must be less than s.length().

  • Function return value:

A temporary string object that holds a substring of at most len characters, starting at position pos of s. If len is too large, it means "to the end" of the string in s.

  • Note:

Positions of characters within a string are numbered starting at 0.



Previous Section
 < Free Open Study > 
Next Section
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)