C++ String Class

C-style strings, called C-strings, consist of characters stored in an array. We will look at them after arrays and pointers.

C++ standard library strings are usuable in a manner similar to the built-in types. Remember, a variable's/object's type determines what operations may be performed on it. Below are some ways C++ strings (variables/objects of type string) can be used.

Notes:

define string str1;
  • str1 is empty.
string str2 = "literal";
  • str2 is initialized to the string literal
string str3("literal");
  • str3 is initialized to the string literal
string str4(n, 'c');
  • str4 is initialized to n copies of char c
output cout << str2
  • result: cout
  • side effect: the characters of str2 are output
input cin >> str2
  • result: cin
  • side effect: characters are input and become the string str2
assign str2 = str1
  • result: str2
  • side effect: str2 gets a new value, a copy of str1
concatenate str1 + str2
  • result: a string object that consists of the characters of str1 followed by the characters of str2.
    Also works if one of str1 or str2 is a C-string.
  • side effect: none
append str1 += str2
  • result: str1
  • side effect: str1 is comprised of its original characters followed by the characters of str2
compare str1 == str2
Also: !=, <, <=, >, and >=
  • result: true or false
  • side effect: none
length str1.length()
  • result: returns the number of elements in str1.
    The member function size() behaves identically. size is the name of the corresponding function for other container types.
  • side effect: none
index str1[index]
  • result: the character at index index in str1
  • side effect: str1 is comprised of its original characters followed by the characters of str2

    Note that indexes start at 0 and so the greatest valid index will be str1.length() - 1, that is the last character in string str1 is str1[str1.length() - 1]
C-string str1.c_str()
  • result: a C-style string version of the string object str1.
    Some C library functions require a C-string, this give a C-string version of the C++ string object.
  • side effect: none
search str1.find(string str2)
str1.find(string str2, size_type index)
  • result: returns the position of str2 in str1.
    The special value string::npos is returned if the find fails.
  • side effect: none

    For the second version the search starts at index index.
substring str1.substr(size_type index, size_type length)
  • result: returns a string comprised of length characters starting at index of str1. If length is omitted the rest of the string is returned.
    An out_of_range exception is thrown if
    index >= str1.length()
    is true. (Indexes start with 0.)
  • side effect: none

A special integer type is provided that is guaranteed to be large enough to represent string's of any length (for the platform). The type is string::size_type. Unless you are doing something unusual (very large strings) you can use an int in its place.

Is a C++ string implemented as a character array? The user of the class doesn't need know the internals of the class, just how to use it. It is up to the implementer of the class to implement it well.