std::basic_string
<string>
class CharT,
class Traits = std::char_traits <CharT>,
class Allocator = std::allocator <CharT>
template<
class CharT,
class Traits = std::char_traits <CharT>
> using basic_string =
std::basic_string<CharT, Traits, std::pmr::polymorphic_allocator <CharT>>;
The class template basic_string
stores and manipulates sequences of character-like objects, which are non-array objects of TrivialType and StandardLayoutType. 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())
, and *(s.begin() + s.size()) has value CharT() (a null terminator)(since C++11); or, equivalently, a pointer to s[0] can be passed to functions that expect a pointer to the first element of an array(until C++11)a null-terminated array(since C++11) of CharT
.
std::basic_string
satisfies the requirements of AllocatorAwareContainer (except that customized construct
/destroy
are not used for construction/destruction of elements), SequenceContainer and ContiguousContainer (since C++17).
If any of Traits::char_type
and Allocator::value_type
is different from CharT
, the program is ill-formed.
std::basic_string
are constexpr: it is possible to create and use std::basic_string
objects in the evaluation of a constant expression.However, std::basic_string
objects generally cannot be constexpr, because any dynamically allocated storage must be released in the same evaluation of constant expression.
Several typedefs for common character types are provided:
<string>
std::string
std::basic_string<char>
std::wstring
std::basic_string<wchar_t>
std::u8string
(C++20)
std::basic_string<char8_t>
std::u16string
(C++11)
std::basic_string<char16_t>
std::u32string
(C++11)
std::basic_string<char32_t>
std::pmr::string
(C++17)
std::pmr::basic_string<char>
std::pmr::wstring
(C++17)
std::pmr::basic_string<wchar_t>
std::pmr::u8string
(C++20)
std::pmr::basic_string<char8_t>
std::pmr::u16string
(C++17)
std::pmr::basic_string<char16_t>
std::pmr::u32string
(C++17)
std::pmr::basic_string<char32_t>
Contents
[edit] Template parameters
[edit] Nested types
traits_type
Traits
value_type
CharT
size_type
Allocator::size_type
(until C++11)difference_type
reference
const_reference
pointer
Allocator::pointer
std::allocator_traits <Allocator>::pointer
(since C++11)const_pointer
Allocator::const_pointer
std::allocator_traits <Allocator>::const_pointer
(since C++11)iterator
LegacyRandomAccessIterator and LegacyContiguousIterator to value_type
LegacyRandomAccessIterator, contiguous_iterator
, and ConstexprIterator to value_type
const_iterator
LegacyRandomAccessIterator and LegacyContiguousIterator to const value_type
(until C++20)LegacyRandomAccessIterator, contiguous_iterator
, and ConstexprIterator to const value_type
reverse_iterator
const_reverse_iterator
[edit] Data members
npos
[static]
the special value size_type(-1), its exact meaning depends on the context
[edit] Member functions
Element access
(public member function) [edit]
basic_string_view
into the entire string (public member function) [edit]
Iterators
Capacity
(public member function) [edit]
Modifiers
(public member function) [edit]
(public member function) [edit]
Search
Operations
(public member function) [edit]
[edit] Non-member functions
(function template) [edit]
(function template) [edit]
Input/output
Numeric conversions
[edit] Literals
std::literals::string_literals
[edit] Helper classes
[edit] Deduction guides (since C++17)
[edit] Iterator invalidation
References, pointers, and iterators referring to the elements of a basic_string
may be invalidated by any standard library function taking a reference to non-const basic_string
as an argument, such as std::getline , std::swap , or operator>>, and by calling non-const member functions, except operator[], at
, data
, front
, back
, begin
, rbegin
, end
, and rend
.
[edit] Notes
Although it is required that customized construct
or destroy
is used when constructing or destroying elements of std::basic_string
until C++23, all implementations only used the default mechanism. The requirement is corrected by P1072R10 to match existing practice.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_string_udls |
201304L |
(C++14) | User-defined literals for string types |
__cpp_lib_starts_ends_with |
201711L |
(C++20) | starts_with , ends_with
|
__cpp_lib_constexpr_string |
201907L |
(C++20) | Constexpr for std::basic_string
|
__cpp_lib_char8_t |
201907L |
(C++20) | std::u8string
|
__cpp_lib_erase_if |
202002L |
(C++20) | erase , erase_if
|
__cpp_lib_string_contains |
202011L |
(C++23) | contains
|
__cpp_lib_string_resize_and_overwrite |
202110L |
(C++23) | resize_and_overwrite
|
__cpp_lib_containers_ranges |
202202L |
(C++23) | Member functions for construction, insertion, and replacement that accept container compatible range |
[edit] Example
#include <iostream> #include <string> int main() { using namespace std::literals; // Creating a string from const char* std::string str1 = "hello"; // Creating a string using string literal auto str2 = "world"s; // Concatenating strings std::string str3 = str1 + " " + str2; // Print out the result std::cout << str3 << '\n'; std::string::size_type pos = str3.find(" "); str1 = str3.substr(pos + 1); // the part after the space str2 = str3.substr(0, pos); // the part till the space std::cout << str1 << ' ' << str2 << '\n'; // Accessing an element using subscript operator[] std::cout << str1[0] << '\n'; str1[0] = 'W'; std::cout << str1 << '\n'; }
Output:
hello world world hello w World
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 530 | C++98 | contiguity of the storage for elements of basic_string was accidently made not required by LWG259 |
required again |
LWG 2861 | C++98 | value_type was Traits::char_type
|
changed to CharT
|
LWG 2994 (P1148R0) |
C++98 | the behavior is undefined if any of Traits::char_type [1] and Allocator::char_type is different from CharT
|
the program is ill-formed in this case |