Summary

Section 21.1 Introduction

  • Class template basic_string provides typical string-manipulation operations.

  • The typedef statement

    
    typedef basic_string< char > string;
    

    creates the alias type string for basic_string<char> (p. 870). A typedef also is provided for the wchar_t type (wstring).

  • To use strings, include C++ Standard Library header <string>.

  • Assigning a single character to a string object is permitted in an assignment statement.

  • strings are not necessarily null terminated.

  • Most string member functions take as arguments a starting subscript location and the number of characters on which to operate.

  • string member functions size and length (p. 871) return the number of characters currently stored in a string.

Section 21.2 string Assignment and Concatenation

  • Class string provides overloaded operator= and function assign (p. 871) for assignments.

  • The subscript operator, [], provides read/write access to any element of a string.

  • string member function at (p. 873) provides checked access (p. 873)—going past either end of the string throws an out_of_range exception. The subscript operator, [], does not provide checked access.

  • The overloaded + and += operators and member function append (p. 873) perform string concatenation.

Section 21.3 Comparing strings

  • Class string provides overloaded ==, !=, <, >, <= and >= operators for string comparisons.

  • string member function compare (p. 875) compares two strings (or substrings) and returns 0 if the strings are equal, a positive number if the first string is lexicographically (p. 875) greater than the second or a negative number if the first string is lexicographically less than the second.

Section 21.4 Substrings

  • string member function substr (p. 876) retrieves a substring from a string.

Section 21.5 Swapping strings

  • string member function swap (p. 876) swaps the contents of two strings.

Section 21.6 string Characteristics

  • string member function capacity (p. 879) returns the total number of characters that can be stored in a string without increasing the amount of memory allocated to the string.

  • string member function max_size (p. 879) returns the maximum size a string can have.

  • string member function resize (p. 879) changes the length of a string.

  • string member function empty returns true if a string is empty.

Section 21.7 Finding Substrings and Characters in a string

  • Class string find functions (p. 881) find, rfind, find_first_of, find_last_of and find_first_not_of locate substrings or characters in a string.

Section 21.8 Replacing Characters in a string

  • string member function erase (p. 881) deletes elements of a string.

  • string member function replace (p. 883) replaces characters in a string.

Section 21.9 Inserting Characters into a string

  • string member function insert (p. 884) inserts characters in a string.

Section 21.10 Conversion to Pointer-Based char* Strings

  • string member function c_str (p. 885) returns a const char* pointing to a null-terminated pointer-based string that contains all the characters in a string.

  • string member function data (p. 885) returns a const char* pointing to a non-null-terminated built-in character array that contains all the characters in a string.

Section 21.11 Iterators

  • Class string provides member functions begin and end (p. 886) to iterate through individual elements.

  • Class string provides member functions rend and rbegin (p. 887) for accessing individual string characters in reverse from the end of a string toward the beginning.

Section 21.12 String Stream Processing

  • Input from a string is supported by type istringstream (p. 887). Output to a string is supported by type ostringstream (p. 887).

  • ostringstream member function str (p. 888) returns the string from the stream.

Section 21.13 C++11 Numeric Conversion Functions

  • C++11’s <string> header now contains functions for converting from numeric values to string objects and from string objects to numeric values.

  • The to_string function (p. 890) returns the string representation of its numeric argument and is overloaded for types int, unsigned int, long, unsigned long, long long, unsigned long long, float, double and long double.

  • C++11 provides eight functions for converting string objects to numeric values. Each function attempts to convert the beginning of its string argument to a numeric value. If no conversion can be performed, an invalid_argument exception occurs. If the result of the convertion is out of range for the function’s return type, an out_of_range exception occurs.

  • Each function that converts a string to an integral type receives three parameters—a string containing the characters to convert, a pointer to a size_t variable where the function stores the index of the first character that was not converted (a null pointer, by default) and an int from 2 to 36 representing the number’s base (base 10, by default).

  • The functions that convert strings to floating-point types each receive two parameters—a string containing the characters to convert and a pointer to a size_t variable where the function stores the index of the first character that was not converted (a null pointer, by default).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset