##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::basic_string

From cppreference.com
< cpp‎ | string

Defined in header `<string>` |  |
template< class CharT, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT> > class basic_string; |  |

The class template `basic_string` stores and manipulates sequences of char-like objects. 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.

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 `CharT[]` array. | (since C++11)

`std::basic_string` satisfies the requirements of `AllocatorAwareContainer`.

Several typedefs for common character types are provided:

Defined in header `<string>`
Type | Definition
std::string | std::basic_string<char>
std::wstring | std::basic_string<wchar_t>
std::u16string (C++11) | std::basic_string<char16_t>
std::u32string (C++11) | std::basic_string<char32_t>

### [edit] Template parameters

CharT | - | character type
Traits | - | traits class specifying the operations on the character type
Allocator | - | `Allocator` type used to allocate internal storage

### [edit] Member types

Member type | Definition
`traits_type` | `Traits`
`value_type` | `Traits::char_type`
`allocator_type` | `Allocator` [edit]
`size_type` | Unsigned integral type (usually std::size_t) [edit]
`difference_type` | Signed integer type (usually std::ptrdiff_t) [edit]
`reference` |
`Allocator::reference` | (until C++11)
`value_type&` | (since C++11)
[edit]
`const_reference` |
`Allocator::const_reference` | (until C++11)
`const value_type&` | (since C++11)
[edit]
`pointer` |
`Allocator::pointer` | (until C++11)
std::allocator_traits<Allocator>::pointer | (since C++11)
[edit]
`const_pointer` |
`Allocator::const_pointer` | (until C++11)
std::allocator_traits<Allocator>::const_pointer | (since C++11)
[edit]
`iterator` | `RandomAccessIterator` [edit]
`const_iterator` | Constant random access iterator [edit]
`reverse_iterator` | std::reverse_iterator<iterator> [edit]
`const_reverse_iterator` | std::reverse_iterator<const_iterator> [edit]

### [edit] Member functions

(constructor) | constructs a `basic_string` (public member function) [edit]
operator= | assigns values to the string (public member function) [edit]
assign | assign characters to a string (public member function) [edit]
get_allocator | returns the associated allocator (public member function) [edit]
##### Element access
at | access specified character with bounds checking (public member function) [edit]
operator[] | access specified character (public member function) [edit]
front (C++11) | accesses the first character (public member function) [edit]
back (C++11) | accesses the last character (public member function) [edit]
data | returns a pointer to the first character of a string (public member function) [edit]
c_str | returns a non-modifiable standard C character array version of the string (public member function) [edit]
##### Iterators
begin cbegin (C++11) | returns an iterator to the beginning (public member function) [edit]
end cend (C++11) | returns an iterator to the end (public member function) [edit]
rbegin crbegin (C++11) | returns a reverse iterator to the beginning (public member function) [edit]
rend crend (C++11) | returns a reverse iterator to the end (public member function) [edit]
##### Capacity
empty | checks whether the string is empty (public member function) [edit]
size length | returns the number of characters (public member function) [edit]
max_size | returns the maximum number of characters (public member function) [edit]
reserve | reserves storage (public member function) [edit]
capacity | returns the number of characters that can be held in currently allocated storage (public member function) [edit]
shrink_to_fit (C++11) | reduces memory usage by freeing unused memory (public member function) [edit]
##### Operations
clear | clears the contents (public member function) [edit]
insert | inserts characters (public member function) [edit]
erase | removes characters (public member function) [edit]
push_back | appends a character to the end (public member function) [edit]
pop_back (C++11) | removes the last character (public member function) [edit]
append | appends characters to the end (public member function) [edit]
operator+= | appends characters to the end (public member function) [edit]
compare | compares two strings (public member function) [edit]
replace | replaces specified portion of a string (public member function) [edit]
substr | returns a substring (public member function) [edit]
copy | copies characters (public member function) [edit]
resize | changes the number of characters stored (public member function) [edit]
swap | swaps the contents (public member function) [edit]
##### Search
find | find characters in the string (public member function) [edit]
rfind | find the last occurrence of a substring (public member function) [edit]
find_first_of | find first occurrence of characters (public member function) [edit]
find_first_not_of | find first absence of characters (public member function) [edit]
find_last_of | find last occurrence of characters (public member function) [edit]
find_last_not_of | find last absence of characters (public member function) [edit]
### Constants
npos [static] | special value. The exact meaning depends on the context (public static member constant) [edit]

### [edit] Non-member functions

operator+ | concatenates two strings or a string and a char (function template) [edit]
operator==operator!=operator<operator>operator<=operator>= | lexicographically compares two strings (function template) [edit]
std::swap(std::basic_string) | specializes the std::swap algorithm (function template) [edit]
##### Input/output
operator<<operator>> | performs stream input and output on strings (function template) [edit]
getline | read data from an I/O stream into a string (function) [edit]
##### Numeric conversions
stoi stol stoll (C++11)(C++11)(C++11) | converts a string to a signed integer (function) [edit]
stoul stoull (C++11)(C++11) | converts a string to an unsigned integer (function) [edit]
stof stod stold (C++11)(C++11)(C++11) | converts a string to a floating point value (function) [edit]
to_string (C++11) | converts an integral or floating point value to `string` (function) [edit]
to_wstring (C++11) | converts an integral or floating point value to `wstring` (function) [edit]

### [edit] Literals

Defined in inline namespace `std::literals::string_literals`
operator""s (C++14) | Converts a character array literal to `basic_string` (function) [edit]

### [edit] Helper classes

std::hash<std::string>std::hash<std::u16string>std::hash<std::u32string>std::hash<std::wstring> (C++11)(C++11)(C++11)(C++11) | hash support for strings (class template specialization) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string&oldid=72362"