Difference between revisions of "cpp/string/basic string"
m (→Notes: ~FTM) |
(Added LWG issue #2861 DR.) |
||
(38 intermediate revisions by 17 users not shown) | |||
Line 21: | Line 21: | ||
{{dcl end}} | {{dcl end}} | ||
− | The class template {{tt|basic_string}} stores and manipulates sequences of [[cpp/language/types#Character types|character]]-like objects, which are non-array objects of | + | The class template {{tt|basic_string}} stores and manipulates sequences of [[cpp/language/types#Character types|character]]-like objects, which are non-array objects of {{named req|TrivialType}} and {{named req|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 {{tt|Traits}} template parameter - a specialization of {{lc|std::char_traits}} or a compatible traits class. |
− | The elements of a {{tt|basic_string}} are stored contiguously, that is, for a {{tt|basic_string}} {{c|s}}, {{c|1=&*(s.begin() + n) == &*s.begin() + n}} for any {{c|n}} in {{ | + | The elements of a {{tt|basic_string}} are stored contiguously, that is, for a {{tt|basic_string}} {{c|s}}, {{c|1=&*(s.begin() + n) == &*s.begin() + n}} for any {{c|n}} in {{range|0|s.size()}}{{rev inl|since=c++11|, and {{c|1=*(s.begin() + s.size())}} has value {{c|1=CharT()}} (a null terminator)}}; or, equivalently, a pointer to {{c|s[0]}} can be passed to functions that expect a pointer to the first element of {{rev inl|until=c++11|an array}}{{rev inl|since=c++11|a null-terminated array}} of {{tt|CharT}}. |
{{tt|std::basic_string}} satisfies the requirements of {{named req|AllocatorAwareContainer}} (except that customized {{tt|construct}}/{{tt|destroy}} are not used for construction/destruction of elements), {{named req|SequenceContainer}}{{rev inl|since=c++17| and {{named req|ContiguousContainer}}}}. | {{tt|std::basic_string}} satisfies the requirements of {{named req|AllocatorAwareContainer}} (except that customized {{tt|construct}}/{{tt|destroy}} are not used for construction/destruction of elements), {{named req|SequenceContainer}}{{rev inl|since=c++17| and {{named req|ContiguousContainer}}}}. | ||
+ | |||
+ | If any of {{tt|Traits::char_type}} and {{tt|Allocator::char_type}} is different from {{tt|CharT}}, the program is ill-formed. | ||
{{rrev|since=c++20| | {{rrev|since=c++20| | ||
Line 37: | Line 39: | ||
{{dsc header|string}} | {{dsc header|string}} | ||
{{dsc hitem|Type|Definition}} | {{dsc hitem|Type|Definition}} | ||
− | {{dsc|{{ | + | {{dsc|{{tt|std::string}}|{{c/core|std::basic_string<char>}}}} |
− | {{dsc|{{ | + | {{dsc|{{tt|std::wstring}}|{{c/core|std::basic_string<wchar_t>}}}} |
− | {{dsc|{{ | + | {{dsc|{{tt|std::u8string}} {{mark c++20}}|{{c/core|std::basic_string<char8_t>}}}} |
− | {{dsc|{{ | + | {{dsc|{{tt|std::u16string}} {{mark c++11}}|{{c/core|std::basic_string<char16_t>}}}} |
− | {{dsc|{{ | + | {{dsc|{{tt|std::u32string}} {{mark c++11}}|{{c/core|std::basic_string<char32_t>}}}} |
− | {{dsc|{{ | + | {{dsc|{{tt|std::pmr::string}} {{mark c++17}}|{{c/core|std::pmr::basic_string<char>}}}} |
− | {{dsc|{{ | + | {{dsc|{{tt|std::pmr::wstring}} {{mark c++17}}|{{c/core|std::pmr::basic_string<wchar_t>}}}} |
− | {{dsc|{{ | + | {{dsc|{{tt|std::pmr::u8string}} {{mark c++20}}|{{c/core|std::pmr::basic_string<char8_t>}}}} |
− | {{dsc|{{ | + | {{dsc|{{tt|std::pmr::u16string}} {{mark c++17}}|{{c/core|std::pmr::basic_string<char16_t>}}}} |
− | {{dsc|{{ | + | {{dsc|{{tt|std::pmr::u32string}} {{mark c++17}}|{{c/core|std::pmr::basic_string<char32_t>}}}} |
{{dsc end}} | {{dsc end}} | ||
Line 56: | Line 58: | ||
{{par end}} | {{par end}} | ||
− | === | + | ===Nested types=== |
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc hitem| | + | {{dsc hitem|Type|Definition}} |
{{dsc|{{tt|traits_type}}|{{tt|Traits}}}} | {{dsc|{{tt|traits_type}}|{{tt|Traits}}}} | ||
− | {{dsc|{{tt|value_type}}|{{tt|CharT | + | {{dsc|{{tt|value_type}}|{{tt|CharT}}}} |
{{dsc inc|cpp/container/dsc allocator_type|basic_string}} | {{dsc inc|cpp/container/dsc allocator_type|basic_string}} | ||
{{dsc inc|cpp/container/dsc size_type|basic_string}} | {{dsc inc|cpp/container/dsc size_type|basic_string}} | ||
Line 72: | Line 74: | ||
{{dsc inc|cpp/container/dsc reverse_iterator|basic_string}} | {{dsc inc|cpp/container/dsc reverse_iterator|basic_string}} | ||
{{dsc inc|cpp/container/dsc const_reverse_iterator|basic_string}} | {{dsc inc|cpp/container/dsc const_reverse_iterator|basic_string}} | ||
+ | {{dsc end}} | ||
+ | |||
+ | ===Data members=== | ||
+ | {{dsc begin}} | ||
+ | {{cpp/string/basic string/dsc npos}} | ||
{{dsc end}} | {{dsc end}} | ||
Line 106: | Line 113: | ||
{{dsc inc|cpp/string/basic_string/dsc shrink_to_fit}} | {{dsc inc|cpp/string/basic_string/dsc shrink_to_fit}} | ||
− | {{dsc h2| | + | {{dsc h2|Modifiers}} |
{{dsc inc|cpp/string/basic_string/dsc clear}} | {{dsc inc|cpp/string/basic_string/dsc clear}} | ||
{{dsc inc|cpp/string/basic_string/dsc insert}} | {{dsc inc|cpp/string/basic_string/dsc insert}} | ||
Line 116: | Line 123: | ||
{{dsc inc|cpp/string/basic_string/dsc append_range}} | {{dsc inc|cpp/string/basic_string/dsc append_range}} | ||
{{dsc inc|cpp/string/basic_string/dsc operator+{{=}}}} | {{dsc inc|cpp/string/basic_string/dsc operator+{{=}}}} | ||
− | |||
− | |||
− | |||
− | |||
{{dsc inc|cpp/string/basic_string/dsc replace}} | {{dsc inc|cpp/string/basic_string/dsc replace}} | ||
{{dsc inc|cpp/string/basic_string/dsc replace_with_range}} | {{dsc inc|cpp/string/basic_string/dsc replace_with_range}} | ||
− | |||
{{dsc inc|cpp/string/basic_string/dsc copy}} | {{dsc inc|cpp/string/basic_string/dsc copy}} | ||
{{dsc inc|cpp/string/basic_string/dsc resize}} | {{dsc inc|cpp/string/basic_string/dsc resize}} | ||
Line 136: | Line 138: | ||
{{dsc inc|cpp/string/basic_string/dsc find_last_not_of}} | {{dsc inc|cpp/string/basic_string/dsc find_last_not_of}} | ||
− | {{dsc | + | {{dsc h2|Operations}} |
− | {{dsc inc|cpp/string/basic_string/dsc | + | {{dsc inc|cpp/string/basic_string/dsc compare}} |
+ | {{dsc inc|cpp/string/basic_string/dsc starts_with}} | ||
+ | {{dsc inc|cpp/string/basic_string/dsc ends_with}} | ||
+ | {{dsc inc|cpp/string/basic_string/dsc contains}} | ||
+ | {{dsc inc|cpp/string/basic_string/dsc substr}} | ||
{{dsc end}} | {{dsc end}} | ||
Line 170: | Line 176: | ||
===[[cpp/string/basic_string/deduction_guides|Deduction guides]] {{mark since c++17}}=== | ===[[cpp/string/basic_string/deduction_guides|Deduction guides]] {{mark since c++17}}=== | ||
+ | |||
+ | ===Iterator invalidation=== | ||
+ | References, pointers, and iterators referring to the elements of a {{tt|basic_string}} may be invalidated by any standard library function taking a reference to non-const {{tt|basic_string}} as an argument, such as {{lc|std::getline}}, {{lc|std::swap}}, or {{ltt|cpp/string/basic_string/operator_ltltgtgt|operator>>}}, and by calling non-const member functions, except {{ltt|cpp/string/basic_string/operator_at|operator[]}}, {{rlt|at}}, {{rlt|data}}, {{rlt|front}}, {{rlt|back}}, {{rlt|begin}}, {{rlt|rbegin}}, {{rlt|end}}, and {{rlt|rend}}. | ||
===Notes=== | ===Notes=== | ||
Line 176: | Line 185: | ||
{{ftm begin|std=1|value=1|comment=1|sort=1}} | {{ftm begin|std=1|value=1|comment=1|sort=1}} | ||
{{ftm|std=C++14|value=201304L|__cpp_lib_string_udls|[[#Literals|User-defined literals for string types]]}} | {{ftm|std=C++14|value=201304L|__cpp_lib_string_udls|[[#Literals|User-defined literals for string types]]}} | ||
+ | {{ftm|std=C++20|value=201711L|__cpp_lib_starts_ends_with|{{rlt|starts_with}}, {{rlt|ends_with}}}} | ||
+ | {{ftm|std=C++20|value=201907L|__cpp_lib_constexpr_string|Constexpr for {{tt|std::basic_string}}}} | ||
+ | {{ftm|std=C++20|value=201907L|__cpp_lib_char8_t|{{tt|std::u8string}}}} | ||
+ | {{ftm|std=C++20|value=202002L|__cpp_lib_erase_if|{{rlt|erase2|erase}}, {{rlt|erase2|erase_if}}}} | ||
{{ftm|std=C++23|value=202011L|__cpp_lib_string_contains|{{rlt|contains}}}} | {{ftm|std=C++23|value=202011L|__cpp_lib_string_contains|{{rlt|contains}}}} | ||
{{ftm|std=C++23|value=202110L|__cpp_lib_string_resize_and_overwrite|{{rlt|resize_and_overwrite}}}} | {{ftm|std=C++23|value=202110L|__cpp_lib_string_resize_and_overwrite|{{rlt|resize_and_overwrite}}}} | ||
− | {{ftm | + | {{ftm|std=C++23|value=202202L|__cpp_lib_containers_ranges|Member functions for construction, insertion, and replacement that accept {{ls|cpp/ranges/to#container compatible range}}}} |
{{ftm end}} | {{ftm end}} | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | |||
|code= | |code= | ||
#include <iostream> | #include <iostream> | ||
Line 224: | Line 236: | ||
===Defect reports=== | ===Defect reports=== | ||
{{dr list begin}} | {{dr list begin}} | ||
− | {{dr list item|wg=lwg|dr=530|std=C++98|before=contiguity of the storage for elements of {{tt|basic_string}} was not required|after=required}} | + | {{dr list item|wg=lwg|dr=530|std=C++98|before=contiguity of the storage for elements of {{tt|basic_string}}<br>was accidently made not required by {{wg21|LWG259}}|after=required again}} |
+ | {{dr list item|wg=lwg|dr=2861|std=C++98|before={{tt|value_type}} was {{tt|Traits::char_type}}|after=changed to {{tt|CharT}}}} | ||
+ | {{dr list item|wg=lwg|dr=2994|paper=P1148R0|std=C++98|before=the behavior is undefined if any of {{tt|Traits::char_type}}<ref>The {{tt|Traits::char_type}} case is fixed in {{stddoc|P1148R0}}.</ref><br>and {{tt|Allocator::char_type}} is different from {{tt|CharT}}|after=the program is<br>ill-formed in this case}} | ||
{{dr list end}} | {{dr list end}} | ||
+ | <references/> | ||
===See also=== | ===See also=== |
Latest revision as of 18:05, 10 November 2024
Defined in header <string>
|
||
template< class CharT, |
(1) | |
namespace pmr { template< |
(2) | (since C++17) |
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::char_type
is different from CharT
, the program is ill-formed.
Member functions of However, |
(since C++20) |
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::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
CharT | - | character type |
Traits | - | traits class specifying the operations on the character type |
Allocator | - | Allocator type used to allocate internal storage |
[edit] Nested types
Type | Definition | ||||
traits_type
|
Traits
| ||||
value_type
|
CharT
| ||||
allocator_type
|
Allocator
| ||||
size_type
|
| ||||
difference_type
|
| ||||
reference
|
value_type& | ||||
const_reference
|
const value_type& | ||||
pointer
|
| ||||
const_pointer
|
| ||||
iterator
|
| ||||
const_iterator
|
| ||||
reverse_iterator
|
std::reverse_iterator<iterator> | ||||
const_reverse_iterator
|
std::reverse_iterator<const_iterator> |
[edit] Data members
constexpr size_type npos [static]
|
the special value size_type(-1), its exact meaning depends on the context |
[edit] Member functions
constructs a basic_string (public member function) | |
destroys the string, deallocating internal storage if used (public member function) | |
assigns values to the string (public member function) | |
assign characters to a string (public member function) | |
(C++23) |
assign a range of characters to a string (public member function) |
returns the associated allocator (public member function) | |
Element access | |
accesses the specified character with bounds checking (public member function) | |
accesses the specified character (public member function) | |
(DR*) |
accesses the first character (public member function) |
(DR*) |
accesses the last character (public member function) |
returns a pointer to the first character of a string (public member function) | |
returns a non-modifiable standard C character array version of the string (public member function) | |
(C++17) |
returns a non-modifiable basic_string_view into the entire string (public member function) |
Iterators | |
(C++11) |
returns an iterator to the beginning (public member function) |
(C++11) |
returns an iterator to the end (public member function) |
(C++11) |
returns a reverse iterator to the beginning (public member function) |
(C++11) |
returns a reverse iterator to the end (public member function) |
Capacity | |
checks whether the string is empty (public member function) | |
returns the number of characters (public member function) | |
returns the maximum number of characters (public member function) | |
reserves storage (public member function) | |
returns the number of characters that can be held in currently allocated storage (public member function) | |
(DR*) |
reduces memory usage by freeing unused memory (public member function) |
Modifiers | |
clears the contents (public member function) | |
inserts characters (public member function) | |
(C++23) |
inserts a range of characters (public member function) |
removes characters (public member function) | |
appends a character to the end (public member function) | |
(DR*) |
removes the last character (public member function) |
appends characters to the end (public member function) | |
(C++23) |
appends a range of characters to the end (public member function) |
appends characters to the end (public member function) | |
replaces specified portion of a string (public member function) | |
(C++23) |
replaces specified portion of a string with a range of characters (public member function) |
copies characters (public member function) | |
changes the number of characters stored (public member function) | |
(C++23) |
changes the number of characters stored and possibly overwrites indeterminate contents via user-provided operation (public member function) |
swaps the contents (public member function) | |
Search | |
finds the first occurrence of the given substring (public member function) | |
find the last occurrence of a substring (public member function) | |
find first occurrence of characters (public member function) | |
find first absence of characters (public member function) | |
find last occurrence of characters (public member function) | |
find last absence of characters (public member function) | |
Operations | |
compares two strings (public member function) | |
(C++20) |
checks if the string starts with the given prefix (public member function) |
(C++20) |
checks if the string ends with the given suffix (public member function) |
(C++23) |
checks if the string contains the given substring or character (public member function) |
returns a substring (public member function) |
[edit] Non-member functions
concatenates two strings, a string and a char, or a string and string_view (function template) | |
(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20) |
lexicographically compares two strings (function template) |
specializes the std::swap algorithm (function template) | |
erases all elements satisfying specific criteria (function template) | |
Input/output | |
performs stream input and output on strings (function template) | |
read data from an I/O stream into a string (function template) | |
Numeric conversions | |
(C++11)(C++11)(C++11) |
converts a string to a signed integer (function) |
(C++11)(C++11) |
converts a string to an unsigned integer (function) |
(C++11)(C++11)(C++11) |
converts a string to a floating point value (function) |
(C++11) |
converts an integral or floating-point value to string (function) |
(C++11) |
converts an integral or floating-point value to wstring (function) |
[edit] Literals
Defined in inline namespace
std::literals::string_literals | |
(C++14) |
converts a character array literal to basic_string (function) |
[edit] Helper classes
(C++11) |
hash support for strings (class template specialization) |
[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 |
[edit] See also
(C++17) |
read-only string view (class template) |
[edit] External links
C++ string handling |