www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 906]

Summary

  • C++ class template basic_string provides typical string-manipulation operations such as copying, searching, etc.

  • The typedef statement

         typedef basic_string< char > string;
    

    creates the alias type string for basic_string< char >. A typedef also is provided for the wchar_t type. Type wchar_t normally stores two-byte (16-bit) characters for supporting other character sets. The size of wchar_t is not fixed by the standard.

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

  • Class string provides no constructors that convert from int or char to 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.

  • Class string provides overloaded operator= and member function assign for string assignments.

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

  • string member function at provides checked accessgoing past either end of the string throws an out_of_range exception. The subscript operator, [], does not provide checked access.

  • Class string provides the overloaded + and += operators and member function append to perform string concatenation.

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

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

  • string member function substr retrieves a substring from a string.

  • string member function swap swaps the contents of two strings.

  • string member functions size and length return the size or length of a string (i.e., the number of characters currently stored in the string).

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

  • string member function max_size returns the maximum size a string can have.

  • string member function resize changes the length of a string.

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

  • string member function erase deletes elements of a string.

  • string member function replace replaces characters in a string.

  • string member function insert inserts characters in a string.

  • string member function c_str returns a const char * pointing to a null-terminated C-style character string that contains all the characters in a string.

  • string member function data returns a const char * pointing to a non-null-terminated C-style character array that contains all the characters in a string.


  • [Page 907]
  • Class string provides member functions end and begin to iterate through individual elements.

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

  • Input from a string is supported by type istringstream. Output to a string is supported by type ostringstream.

  • ostringstream member function str returns a string copy of a string.


Previous Page
Next Page