Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


C++ String

A String is a sequence of characters. The standard string class provides interface similar to standard byte container, but adding features specifically designed to operate with strings of single-byte characters.

The string class is an instantiation of the basic_string class template that uses char (i.e., bytes) as its character type.

Syntax

typedef basic_string<char> string;

C++ - String Functions

The C++ string container has a number of member functions which are listed below:

Capacity

FunctionsDescription
empty() Checks whether the string is empty or not.
length() Returns the length of the string in terms of bytes.
size() Returns the length of the string in terms of bytes.
resize() Changes the size of the string to a length of specified number of characters.
max_size() Returns the maximum length of the string.
clear() Clears all characters of the string.
capacity() Returns size of allocated space to the string.
shrink_to_fit() Reduces the capacity of the string equal to fit its size.
reserve() Requests to reserve the string capacity be at least enough to contain n characters.

Element Access

FunctionsDescription
at() Access a character of the string.
operator[]() Access a character of the string.
front() Access first character of the string.
back() Access last character of the string.

Iterators

FunctionsDescription
begin() Returns iterator pointing to the first character of the string.
end() Returns iterator pointing to the past-the-last character of the string.
rbegin() Returns reverse iterator to the last character of the string.
rend() Returns reverse iterator to the character preceding the first character of the string.
cbegin() Returns const_iterator pointing to the first character of the string.
cend() Returns const_iterator pointing to the past-the-last character of the string.
crbegin() Returns const_reverse_iterator to the last character of the string.
crend() Returns const_reverse_iterator to the character preceding the first character of the string.

Modifiers

FunctionsDescription
pop_back() Deletes last character of the string.
push_back() Adds a new character at the end of the string.
swap() Exchanges content of two strings.

String Operations

FunctionsDescription
compare() Compares two string objects.

Non-member function overloads

FunctionsDescription
swap() Exchanges content of two strings.

5