A C++ string is an object of the class string
, which is defined in the header file <string> and which is in the standard namespace. The string
class has several constructors that may be called (explicitly or implicitly) to create a string object.
Examples:
char text[21] = "goodbye";
string s1; // Create an empty C++ string of length 0, equal to "" (default constructor)
string s2("hello"); // Initialize string with C string literal
string s3 = "hello"; // Initialize string with C string literal
string s4(text); // Initialize string with char array that contains a C string
string s5 = text; // Initialize string with char array that contains a C string
string s6(s2); // Initialize string with another C++ string (copy constructor)
string s7 = s2; // Initialize string with another C++ string (copy constructor)
Here is another example of declaring a C++ string:
string name = "Karen";
name
is a string
object with several data members. The data member p
is a pointer to (contains the address of) the first character in a dynamically-allocated array of characters. The data member length
contains the length of the string. The data member capacity
contains the number of valid characters that may currently be stored in the array.
A "null string" or "empty string" is a string with a length
of 0:
The length of an empty string is 0.
The subscript operator may be used to access the individual characters of a C++ string:
cout << s3[1] << endl; // Prints the character 'e', the second character in the string "Hello"
The reason that this works is a C++ feature called operator overloading. Using the subscript operator with a C++ string
object actually calls a special member function named operator[]
that has been defined as part of the string
class. The subscript specified inside the brackets is passed as an argument to the member function, which then returns the character at that position in the string.
The name of a C++ string
object is never automatically converted to a pointer and you can not use pointer notation with it or perform pointer arithmetic on it.
You can obtain the length of a C++ string using the string
class member functions length()
or size()
. Both of the methods return the data type size_t
(which is an unsigned integer type), the number of valid characters in the string.
Note that if you try to compare an integer loop counter variable with a size_t
return value, you will get a warning from the compiler that you are comparing a signed integer to an unsigned integer. The warning can be resolved by changing the data type of the loop counter to size_t
or by type casting the size_t
return value to int
.
Examples
string s = "Some text";
cout << "String length is " << s.length() << endl; // Length is 9
// Loop through the characters of the string
for (size_t i = 0; i < s.size(); i++)
cout << s[i];
cout << endl;
C++ string
objects may be compared using the standard relational operators ==
, !=
, >
, <
, >=
, and <=
. A C++ string may be compared to either another C++ string or a valid C string, including a string literal. All such relational expressions resolve to the Boolean values true
or false
.
Examples
if (s1 > s2) // Compare two C++ strings
{
...
}
if ("cat" == s2) // Compare C string literal and C++ string
{
...
}
if (s3 != cstr) // Compare C++ string and array that contains a C string
{
...
}
Like subscripting, this works because of operator overloading.
You can assign a C++ string
object, a C string, or a C string literal to a C++ string
object.
Examples
string s1 = "original string";
string s2 = "new string";
char s3[20] = "another string";
s1 = s2; // s1 changed to "new string"
s1 = s3; // s1 changed to "another string"
s1 = "yet another string"; // s1 changed to "yet another string"
Once again, this works because of operator overloading.
The operator +
may be used to concatenate C++ strings. C++ strings, C strings, and string literals may all be concatenated together in any order. The result is a C++ string
object that may be assigned to another C++ string
object, passed to a function that takes a C++ string
object as an argument, printed, etc.
string s1 = "Hello";
string s2 = " good ";
char s3[10] = "friend";
s1 = s1 + ", my " + s2 + s3; // s1 now contains "Hello, my good friend"
There are also other member functions that allow you to append a string
, C string, or single character to the end of a string
.
C++ string
objects are passed and returned by value by default. This results in a copy of the string
object being created.
To save memory (and a likely call to the copy constructor), a string
object is usually passed by reference instead. If the function does not need to modify the string, the object is passed as a reference to a constant object.
Examples
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
void change_string_copy(string);
void change_string(string&);
void print_string(const string&);
int main()
{
string name = "Akeem Spencer";
print_string(name);
change_string_copy(name);
print_string(name);
change_string(name);
print_string(name);
return 0;
}
void change_string_copy(string s)
{
s = "Joy Lambert";
}
void change_string(string& s)
{
s = "Roger Dawkins";
}
void print_string(const string& s)
{
cout << s << endl;
}
Output from the code above would look like this:
Akeem Spencer Akeem Spencer Roger Dawkins
The stream extraction operator >>
may be used to read data into a C++ string
object.
The stream insertion operator <<
may be used to print a C++ string object.
Sometimes you have one type of string, but you want to use a function or method that requires the other type. In that case, it's useful to be able to convert one string type to the other.
You can easily create a C++ string
object from a C string or string literal. Declare the string
object and pass the C string or string literal as a constructor argument.
What if you have a C++ string
object and need to convert it to a C string? The string
class provides a method called c_str()
that returns a pointer to a char
constant, the first character of a C string equivalent to the contents of the C++ string. The C string returned by this method cannot be modified, but it can be used, printed, copied, etc.
string s1 = "My C++ string"; // Define a C++ string
char s2[s1.length()+1]; // Yes, this is legal in C++11!
strcpy(s2, s1.c_str()); // Copies the C string "My C++ string" into the array s2
Use C++ strings whenever possible, since arrays are evil. Unfortunately, it's not always possible to avoid using C strings.