std:: string
Strings are objects that represent sequences of characters.
The standard string class provides support for such objects with an interface similar to that of a standard container of bytes, 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, with its default char_traits and allocator types (see basic_string for more info on the template).
Note that this class handles bytes independently of the encoding used: If used to handle sequences of multi-byte or variable-length characters (such as UTF-8), all members of this class (such as length or size), as well as its iterators, will still operate in terms of bytes (not actual encoded characters).
Member types
member type | definition |
---|---|
value_type | char |
traits_type | char_traits |
allocator_type | allocator |
reference | char& |
const_reference | const char& |
pointer | char* |
const_pointer | const char* |
iterator | a random access iterator to char (convertible to const_iterator) |
const_iterator | a random access iterator to const char |
reverse_iterator | reverse_iterator |
const_reverse_iterator | reverse_iterator |
difference_type | ptrdiff_t |
size_type | size_t |
Member functions
(constructor) Construct string object (public member function) (destructor) String destructor (public member function) operator= String assignment (public member function)
Iterators:
begin Return iterator to beginning (public member function) end Return iterator to end (public member function) rbegin Return reverse iterator to reverse beginning (public member function) rend Return reverse iterator to reverse end (public member function) cbegin Return const_iterator to beginning (public member function) cend Return const_iterator to end (public member function) crbegin Return const_reverse_iterator to reverse beginning (public member function) crend Return const_reverse_iterator to reverse end (public member function)
Capacity:
size Return length of string (public member function) length Return length of string (public member function) max_size Return maximum size of string (public member function) resize Resize string (public member function) capacity Return size of allocated storage (public member function) reserve Request a change in capacity (public member function) clear Clear string (public member function) empty Test if string is empty (public member function) shrink_to_fit Shrink to fit (public member function)
Element access:
operator[] Get character of string (public member function) at Get character in string (public member function) back Access last character (public member function) front Access first character (public member function)
Modifiers:
operator+= Append to string (public member function) append Append to string (public member function) push_back Append character to string (public member function) assign Assign content to string (public member function) insert Insert into string (public member function) erase Erase characters from string (public member function) replace Replace portion of string (public member function) swap Swap string values (public member function) pop_back Delete last character (public member function)
String operations:
c_str Get C string equivalent (public member function) data Get string data (public member function) get_allocator Get allocator (public member function) copy Copy sequence of characters from string (public member function) find Find content in string (public member function) rfind Find last occurrence of content in string (public member function) find_first_of Find character in string (public member function) find_last_of Find character in string from the end (public member function) find_first_not_of Find absence of character in string (public member function) find_last_not_of Find non-matching character in string from the end (public member function) substr Generate substring (public member function) compare Compare strings (public member function)
Member constants
Non-member function overloads
operator+ Concatenate strings (function) relational operators Relational operators for string (function) swap Exchanges the values of two strings (function) operator>> Extract string from stream (function) operator Insert string into stream (function) getline Get line from stream into string (function)
String functions in cpp
Defines the container class template basic_string and various supporting templates.
For more information about basic_string , see basic_string Class
Syntax
Remarks
The C++ language and the C++ Standard Library support two types of strings:
- Null-terminated character arrays often referred to as C strings.
- class template objects, of type basic_string , that handle all char -like template arguments.
Typedefs
Type name | Description |
---|---|
string | A type that describes a specialization of the class template basic_string with elements of type char as a string . |
wstring | A type that describes a specialization of the class template basic_string with elements of type wchar_t as a wstring . |
u16string | A type that describes a specialization of the class template basic_string based on elements of type char16_t . |
u32string | A type that describes a specialization of the class template basic_string based on elements of type char32_t . |
Operators
Operator | Description |
---|---|
operator+ | Concatenates two string objects. |
operator!= | Tests if the string object on the left side of the operator is not equal to the string object on the right side. |
operator== | Tests if the string object on the left side of the operator is equal to the string object on the right side. |
operator< | Tests if the string object on the left side of the operator is less than to the string object on the right side. |
operator | Tests if the string object on the left side of the operator is less than or equal to the string object on the right side. |
operator | A template function that inserts a string into the output stream. |
operator> | Tests if the string object on the left side of the operator is greater than to the string object on the right side. |
operator>= | Tests if the string object on the left side of the operator is greater than or equal to the string object on the right side. |
operator>> | A template function that extracts a string from the input stream. |
Specialized Template Functions
Name | Description |
---|---|
hash | Produces a hash of a string. |
swap | Exchanges the arrays of characters of two strings. |
stod | Converts a character sequence to a double . |
stof | Converts a character sequence to a float . |
stoi | Converts a character sequence to an int . |
stold | Converts a character sequence to a long double . |
stoll | Converts a character sequence to a long long . |
stoul | Converts a character sequence to an unsigned long . |
stoull | Converts a character sequence to an unsigned long long . |
to_string | Converts a value to a string . |
to_wstring | Converts a value to a wide string. |
Functions
Classes
Class | Description |
---|---|
basic_string Class | A class template that describes objects that can store a sequence of arbitrary character-like objects. |
char_traits Struct | A class template that describes attributes associated with a character of type CharType |
Specializations
Name | Description |
---|---|
char_traits Struct | A struct that is a specialization of the template struct char_traits to an element of type char . |
char_traits Struct | A struct that is a specialization of the template struct char_traits to an element of type wchar_t . |
char_traits Struct | A struct that is a specialization of the template struct char_traits to an element of type char16_t . |
char_traits Struct | A struct that is a specialization of the template struct char_traits to an element of type char32_t . |