Note, this article is not finished! You can help by editing this doc page.
Overview
- Simplified
- Detailed
template< class CharT, /* ... */ >
class basic_string;
- Regular
- Polymorphic (since C++17)
template<
class CharT,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>
>
class basic_string;
namespace pmr {
template< class CharT, class Traits = std::char_traits<CharT> >
using basic_string = std::basic_string<CharT, Traits,
std::polymorphic_allocator<CharT>>;
}
A class that is used to store and manipulate sequence of characters.
Memory
The elements of a string are stored contiguously in memory.
This means that a pointer to an element of a string may be passed to any function
that expects a pointer to an element of an array of characters.

Aliases
Type std::string is in fact an alias to: std::basic_string<char>.
Available std::basic_string aliases
| pub | std::string | std::basic_string<char> |
| pub | std::wstring | std::basic_string<wchar_t> |
| pub | std::u8string (od C++20) | std::basic_string<char8_t> |
| pub | std::u16string (od C++11) | std::basic_string<char16_t> |
| pub | std::u32string (od C++11) | std::basic_string<char32_t> |
| pub | std::pmr::string (od C++17) | std::pmr::basic_string<char> |
| pub | std::pmr::wstring (od C++17) | std::pmr::basic_string<wchar_t> |
| pub | std::pmr::u8string (od C++20) | std::pmr::basic_string<char8_t> |
| pub | std::pmr::u16string (od C++17) | std::pmr::basic_string<char16_t> |
| pub | std::pmr::u32string (od C++17) | std::pmr::basic_string<char32_t> |
Technical details
Technical definition of a string
The class template basic_string stores and manipulates sequences of char-like objects,
which are non-array objects of trivial
standard-layout type. The class is dependent neither
on the character type nor on the nature of operations on that type.
The definitions of the operations are supplied via the Traits template parameter - a specialization
of std::char_traits or a compatible traits class. Traits::char_type and CharT
must name the same type; otherwise the program is ill-formed
The elements of a basic_string are stored contiguously, that is, for a basic_string s,
&*(s.begin() + n) == &*s.begin() + n for any n in [ 0, s.size() ), or, equivalently,
a pointer to s[0] can be passed to functions that expect a pointer to the first element
of a null-terminated (od C++11) CharT[] array.
std::basic_string
| Defined in | string |
Template parameters
| pub | CharT | A character type. |
| pub | Traits | Traits class specifying the operations on the character type. |
| pub | Allocator | Allocator type used to allocate internal storage. |
Type names
| pub | traits_type | Traits |
| pub | value_type | CharT |
| pub | allocator_type | Allocator |
| pub | size_type | Allocator::size_type (do C++11) std::allocator_traits<Allocator>::size_type (od C++11) |
| pub | difference_type | Allocator::difference_type (do C++11) std::allocator_traits<Allocator>::difference_type (od C++11) |
| pub | reference | value_type& |
| pub | const_reference | value_type const& |
| pub | pointer | Allocator::pointer (do C++11) std::allocator_traits<Allocator>::pointer (od C++11) |
| pub | const_pointer | Allocator::const_pointer (do C++11) std::allocator_traits<Allocator>::const_pointer (od C++11) |
| pub | iterator |
|
| pub | reverse_iterator | std::reverse_iterator<iterator> |
| pub | const_reverse_iterator | std::reverse_iterator<const_iterator> |
Member functions
| pub | (constructors) | Constructs a |
| pub | (destructor) | Destroys the string, deallocating internal storage if used. |
| pub | operator= | Assigns values to the string. |
| pub | assign | Assigns characters to the string. |
| pub | get_allocator | Returns the associated allocator. |
Element access
| pub | at | Accesses the specified character with bounds checking. |
| pub | operator[] | Accesses the specified character. |
| pub | front (od C++11) | Returns the first character. |
| pub | back (od C++11) | Returns the last character. |
| pub | data | Returns a pointer to the first character of a string. |
| pub | c_str | Returns a non-modifiable standard C character array version of the string. |
| pub | operator basic_string_view (od C++17) | Returns a non-modifiable |
Iterators
| pub | begin cbegin (od C++11) | Returns an |
| pub | end cend (od C++11) | Returns an |
| pub | rbegin crbegin (od C++11) | Returns an reverse |
| pub | rend crend (od C++11) | Returns an reverse |