Difference between revisions of "cpp/string"
m (applied a few minor improvements.) |
m (“NTCTS” is the term used in the standard (I have no idea what “T” stands for, so I can only assume “CT” stands for “character”), defined in paragraph (3.34).) |
||
Line 36: | Line 36: | ||
When a user-defined character container type for {{lc|std::basic_string}}{{rev inl|since=c++17| and {{lc|std::basic_string_view}}}} is used, it is also necessary to provide a corresponding character trait class (which can be a specialization of {{lc|std::char_traits}}). | When a user-defined character container type for {{lc|std::basic_string}}{{rev inl|since=c++17| and {{lc|std::basic_string_view}}}} is used, it is also necessary to provide a corresponding character trait class (which can be a specialization of {{lc|std::char_traits}}). | ||
− | ==== | + | {{anchor|String classes}} |
− | The | + | ====String classes ({{lc|std::string}} etc.)==== |
+ | The class template {{lc|std::basic_string}} generalizes how sequences of characters are manipulated and stored. String creation, manipulation, and destruction are all handled by a convenient set of class methods and related functions. | ||
Several specializations of {{lc|std::basic_string}} are provided for commonly-used types: | Several specializations of {{lc|std::basic_string}} are provided for commonly-used types: | ||
Line 51: | Line 52: | ||
{{rrev|since=c++17| | {{rrev|since=c++17| | ||
− | ==== | + | {{anchor|String view classes}} |
− | The | + | ====String view classes ({{lc|std::string_view}} etc.)==== |
+ | The class template {{lc|std::basic_string_view}} provides a lightweight object that offers read-only access to a string or a part of a string using an interface similar to the interface of {{lc|std::basic_string}}. | ||
Several specializations of {{lc|std::basic_string_view}} are provided for commonly-used types: | Several specializations of {{lc|std::basic_string_view}} are provided for commonly-used types: | ||
Line 67: | Line 69: | ||
====Null-terminated sequence utilities==== | ====Null-terminated sequence utilities==== | ||
− | ''Null-terminated character sequences'' ( | + | ''Null-terminated character sequences'' (NTCTS) are sequences of characters that are terminated by a null character (the value after value-initialization). |
The strings library provides functions to create, inspect, and modify such sequences: | The strings library provides functions to create, inspect, and modify such sequences: | ||
Line 74: | Line 76: | ||
===Relevant libraries=== | ===Relevant libraries=== | ||
− | The [[cpp/locale| | + | The [[cpp/locale|localization library]] provides support for string conversions (e.g. {{lc|std::wstring_convert}} or {{ltt std|cpp/locale/toupper}}) as well as functions that classify characters (e.g. {{ltt std|cpp/locale/isspace}} or {{ltt std|cpp/locale/isdigit}}). |
===Defect reports=== | ===Defect reports=== |
Revision as of 17:47, 1 February 2024
Contents |
Characters
In the C++ standard library, a character is an object which, when treated sequentially, can represent text.
The term means not only objects of character types, but also any value that can be represented by a type that provides the definitions specified in the strings library and following libraries:
(since C++11) |
In the strings library and regular expressions library(since C++11), characters can only be of char-like types, which can be any non-array POD type(until C++20)trivial standard-layout type(since C++20). Therefore, characters are also referred as char-like objects in the strings library and regular expressions library(since C++11).
Some standard library components accept character container types, they are also types used to represent individual characters. Such types are used for one of the template parameters of std::char_traits and the class templates which use std::char_traits.
Library components
The C++ strings library includes support for the following components:
Character traits
Many character-related class templates (such as std::basic_string) need a set of related types and functions to complete the definition of their semantics. These types and functions are provided as a set of member typedef names and functions in the template parameter Traits
used by each such template. The classes which are able to complete those semantics are character traits, and they need to satisfy the CharTraits requirements.
The string library provides the class template std::char_traits that defines types and functions for std::basic_string and std::basic_string_view(since C++17).
The following specializations are defined, all of them satisfy the CharTraits requirements:
Defined in header <string>
|
||
template<> class char_traits<char>; |
||
template<> class char_traits<wchar_t>; |
||
template<> class char_traits<char8_t>; |
(since C++20) | |
template<> class char_traits<char16_t>; |
(since C++11) | |
template<> class char_traits<char32_t>; |
(since C++11) | |
When a user-defined character container type for std::basic_string and std::basic_string_view(since C++17) is used, it is also necessary to provide a corresponding character trait class (which can be a specialization of std::char_traits).
String classes (std::string etc.)
The class template std::basic_string generalizes how sequences of characters are manipulated and stored. String creation, manipulation, and destruction are all handled by a convenient set of class methods and related functions.
Several specializations of std::basic_string are provided for commonly-used types:
Defined in header
<string> | |
Type | Definition |
std::string
|
std::basic_string<char> |
std::wstring
|
std::basic_string<wchar_t> |
std::u8string (since C++20)
|
std::basic_string<char8_t> |
std::u16string (since C++11)
|
std::basic_string<char16_t> |
std::u32string (since C++11)
|
std::basic_string<char32_t> |
String view classes (std::string_view etc.)The class template std::basic_string_view provides a lightweight object that offers read-only access to a string or a part of a string using an interface similar to the interface of std::basic_string. Several specializations of std::basic_string_view are provided for commonly-used types:
|
(since C++17) |
Null-terminated sequence utilities
Null-terminated character sequences (NTCTS) are sequences of characters that are terminated by a null character (the value after value-initialization).
The strings library provides functions to create, inspect, and modify such sequences:
- null-terminated byte strings (NTBS) helper functions (including support of wide character types),
- null-terminated multibyte strings (NTMBS) helper functions.
Relevant libraries
The localization library provides support for string conversions (e.g. std::wstring_convert or std::toupper) as well as functions that classify characters (e.g. std::isspace or std::isdigit).
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 1170 | C++98 | char-like types could be array types | prohibited |
See also
C documentation for Strings library
|