Strings

C++ tutorials that predate C++14 have the student use character arrays and strings at the same time. While these data types fulfill the same purpose, they differ in the way you use them.

The data type of the value "hello world" is actually const char* which is related to a concept you may not have learned yet. For now we shall call that data type character array. When I introduce a hello world program, I use "hello world"s as the value to be displayed on standard output (notice the s suffix). The data type of "hello world"s is string and it supports many more operations than character arrays.

People may use the terms string and character array interchangeably, and in general, they are synonomous; but within the context of C++ data types, a string is not the same as a character array. Others use the terms C string and C++ string to distinguish between a character array and a string.

string is actually the name of a class from the standard C++ library. You may create variables or values using this class after you include the <string> header file.

Here are some operators supported by string:

Operator Description
+ Create new string by concatenating two existing strings
+= Concatenate right-hand string to the left-hand string variable
== Compare for equality
!= Compare for inequality
< Compare for alphabetically lesser
> Compare for alphabetically greater
<= Compare for equality or alphabetically lesser
>= Compare for equality or alphabetically greater

None of these operators are supported by obsolete character arrays which is why I encourage you to take advantage of string literals provided by C++14.

The only operator that a character array and a string share is [] (the subscript operator). This operator allows you to access or modify one of the characters stored in the string or character array.

For the string variable string x = "hello"s: s[0] has the value 'h', s[1] has the value 'e', s[2] has the value 'l', s[3] has the value 'l', and s[4] has the value 'o'.

Each character stored in a string is known as an element of that string, and the subscript operator gives you access to an element via it's index. The index of the first character of a string will always be 0, and the index of the last character is one less than the number of characters in the whole string.

In addition to operators, any string instance (whether it be a literal value or a variable) contains these member functions:

Member function Description
int size(); Returns the number of characters stored in the string
void insert(int index, string s); Inserts string s into the current string starting at index, (the other characters are shifted over to the right)
void erase(int index, int count); Removes count characters from the string starting at index
string substr(int pos, int count); Returns a new string equal to the portion of the current string that begins at index pos and spans count number of characters
int find(string s); Finds the index of first occurrence of string s within the current string. Returns -1 if not found
int rfind(string s); Finds the index of the last occurrence of string s within the current string. Returns -1 if not found