Standard library header <string>
From cppreference.net
此头文件是 字符串 库的组成部分。
包含文件 |
||
|
(C++20)
|
三路比较运算符 支持 | |
|
(C++11)
|
std::initializer_list 类模板 | |
类 |
||
|
描述字符类型属性的类模板
(类模板) |
||
| std:: char_traits < char > | (类模板特化) | |
| std:: char_traits < wchar_t > | (类模板特化) | |
| std:: char_traits < char8_t > (C++20) | (类模板特化) | |
| std:: char_traits < char16_t > (C++11) | (类模板特化) | |
| std:: char_traits < char32_t > (C++11) | (类模板特化) | |
|
存储和操作字符序列
(类模板) |
||
| std::string | std:: basic_string < char > (类型定义) | |
| 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::wstring | std:: basic_string < wchar_t > (类型定义) | |
| std::pmr::basic_string (C++17) | (别名模板) | |
| std::pmr::string (C++17) | std:: pmr :: basic_string < char > (类型定义) | |
| 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 > (类型定义) | |
| std::pmr::wstring (C++17) | std:: pmr :: basic_string < wchar_t > (类型定义) | |
|
(C++11)
|
字符串的哈希支持
(类模板特化) |
|
函数 |
||
概要
#include <compare> #include <initializer_list> namespace std { // 字符特性 template<class CharT> struct char_traits; // 独立实现 template<> struct char_traits<char>; // 独立实现 template<> struct char_traits<char8_t>; // 独立实现 template<> struct char_traits<char16_t>; // 独立实现 template<> struct char_traits<char32_t>; // 独立实现 template<> struct char_traits<wchar_t>; // 独立实现 // basic_string template<class CharT, class Traits = char_traits<CharT>, class Allocator = allocator<CharT>> class basic_string; template<class CharT, class Traits, class Allocator> constexpr basic_string<CharT, Traits, Allocator> operator+(const basic_string<CharT, Traits, Allocator>& lhs, const basic_string<CharT, Traits, Allocator>& rhs); template<class CharT, class Traits, class Allocator> constexpr basic_string<CharT, Traits, Allocator> operator+(basic_string<CharT, Traits, Allocator>&& lhs, const basic_string<CharT, Traits, Allocator>& rhs); template<class CharT, class Traits, class Allocator> constexpr basic_string<CharT, Traits, Allocator> operator+(const basic_string<CharT, Traits, Allocator>& lhs, basic_string<CharT, Traits, Allocator>&& rhs); template<class CharT, class Traits, class Allocator> constexpr basic_string<CharT, Traits, Allocator> operator+(basic_string<CharT, Traits, Allocator>&& lhs, basic_string<CharT, Traits, Allocator>&& rhs); template<class CharT, class Traits, class Allocator> constexpr basic_string<CharT, Traits, Allocator> operator+(const CharT* lhs, const basic_string<CharT, Traits, Allocator>& rhs); template<class CharT, class Traits, class Allocator> constexpr basic_string<CharT, Traits, Allocator> operator+(const CharT* lhs, basic_string<CharT, Traits, Allocator>&& rhs); template<class CharT, class Traits, class Allocator> constexpr basic_string<CharT, Traits, Allocator> operator+(CharT lhs, const basic_string<CharT, Traits, Allocator>& rhs); template<class CharT, class Traits, class Allocator> constexpr basic_string<CharT, Traits, Allocator> operator+(CharT lhs, basic_string<CharT, Traits, Allocator>&& rhs); template<class CharT, class Traits, class Allocator> constexpr basic_string<CharT, Traits, Allocator> operator+(const basic_string<CharT, Traits, Allocator>& lhs, const CharT* rhs); template<class CharT, class Traits, class Allocator> constexpr basic_string<CharT, Traits, Allocator> operator+(basic_string<CharT, Traits, Allocator>&& lhs, const CharT* rhs); template<class CharT, class Traits, class Allocator> constexpr basic_string<CharT, Traits, Allocator> operator+(const basic_string<CharT, Traits, Allocator>& lhs, CharT rhs); template<class CharT, class Traits, class Allocator> constexpr basic_string<CharT, Traits, Allocator> operator+(basic_string<CharT, Traits, Allocator>&& lhs, CharT rhs); template<class CharT, class Traits, class Allocator> constexpr basic_string<CharT, Traits, Allocator> operator+(const basic_string<CharT, Traits, Allocator>& lhs, type_identity_t<basic_string_view<CharT, Traits>> rhs); template<class CharT, class Traits, class Allocator> constexpr basic_string<CharT, Traits, Allocator> operator+(basic_string<CharT, Traits, Allocator>&& lhs, type_identity_t<basic_string_view<CharT, Traits>> rhs); template<class CharT,
类模板 std::char_traits
namespace std { template<> struct char_traits<char> { using char_type = char; using int_type = int; using off_type = streamoff; using pos_type = streampos; using state_type = mbstate_t; using comparison_category = strong_ordering; static constexpr void assign(char_type& c1, const char_type& c2) noexcept; static constexpr bool eq(char_type c1, char_type c2) noexcept; static constexpr bool lt(char_type c1, char_type c2) noexcept; static constexpr int compare(const char_type* s1, const char_type* s2, size_t n); static constexpr size_t length(const char_type* s); static constexpr const char_type* find(const char_type* s, size_t n, const char_type& a); static constexpr char_type* move(char_type* s1, const char_type* s2, size_t n); static constexpr char_type* copy(char_type* s1, const char_type* s2, size_t n); static constexpr char_type* assign(char_type* s, size_t n, char_type a); static constexpr int_type not_eof(int_type c) noexcept; static constexpr char_type to_char_type(int_type c) noexcept; static constexpr int_type to_int_type(char_type c) noexcept; static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept; static constexpr int_type eof() noexcept; }; template<> struct char_traits<char8_t> { using char_type = char8_t; using int_type = unsigned int; using off_type = streamoff; using pos_type = u8streampos; using state_type = mbstate_t; using comparison_category = strong_ordering; static constexpr void assign(char_type& c1, const char_type& c2) noexcept; static constexpr bool eq(char_type c1, char_type c2) noexcept; static constexpr bool lt(char_type c1, char_type c2) noexcept; static constexpr int compare(const char_type* s1, const char_type* s2, size_t n); static constexpr size_t length(const char_type* s); static constexpr const char_type* find(const char_type* s, size_t n, const char_type& a); static constexpr char_type* move(char_type* s1, const char_type* s2, size_t n); static constexpr char_type* copy(char_type* s1, const char_type* s2, size_t n); static constexpr char_type* assign(char_type* s, size_t n, char_type a); static constexpr int_type not_eof(int_type c) noexcept; static constexpr char_type to_char_type(int_type c) noexcept; static constexpr int_type to_int_type(char_type c) noexcept; static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept; static constexpr int_type eof() noexcept; }; template<> struct char_traits<char16_t> { using char_type = char16_t; using int_type = uint_least16_t; using off_type = streamoff; using pos_type = u16streampos; using state_type = mbstate_t; using comparison_category = strong_ordering; static constexpr void assign(char_type& c1, const char_type& c2) noexcept; static constexpr bool eq(char_type c1, char_type c2) noexcept; static constexpr bool lt(char_type c1, char_type c2) noexcept; static constexpr int compare(const char_type* s1, const char_type* s2, size_t n); static constexpr size_t length(const char_type* s<span class="br
类模板 std::basic_string
namespace std { template<class CharT, class Traits = char_traits<CharT>, class Allocator = allocator<CharT>> class basic_string { public: // 类型定义 using traits_type = Traits; using value_type = CharT; using allocator_type = Allocator; using size_type = typename allocator_traits<Allocator>::size_type; using difference_type = typename allocator_traits<Allocator>::difference_type; using pointer = typename allocator_traits<Allocator>::pointer; using const_pointer = typename allocator_traits<Allocator>::const_pointer; using reference = value_type&; using const_reference = const value_type&; using iterator = /* 实现定义 */; using const_iterator = /* 实现定义 */; using reverse_iterator = std::reverse_iterator<iterator>; using const_reverse_iterator = std::reverse_iterator<const_iterator>; static constexpr size_type npos = size_type(-1); // 构造/复制/销毁 constexpr basic_string() noexcept(noexcept(Allocator())) : basic_string(Allocator()) { } constexpr explicit basic_string(const Allocator& a) noexcept; constexpr basic_string(const basic_string& str); constexpr basic_string(basic_string&& str) noexcept; constexpr basic_string(const basic_string& str, size_type pos, const Allocator& a = Allocator()); constexpr basic_string(const basic_string& str, size_type pos, size_type n, const Allocator& a = Allocator()); constexpr basic_string(basic_string&& str, size_type pos, const Allocator& a = Allocator()); constexpr basic_string(basic_string&& str, size_type pos, size_type n, const Allocator& a = Allocator()); template<class T> constexpr basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); template<class T> constexpr explicit basic_string(const T& t, const Allocator& a = Allocator()); constexpr basic_string(const CharT* s, size_type n, const Allocator& a = Allocator()); constexpr basic_string(const CharT* s, const Allocator& a = Allocator()); basic_string(nullptr_t) = delete; constexpr basic_string(size_type n, CharT c, const Allocator& a = Allocator()); template<class InputIt> constexpr basic_string(InputIt begin, InputIt end, const Allocator& a = Allocator()); template<container-compatible-range<CharT> R> constexpr basic_string(from_range_t, R&& rg, const Allocator& a = Allocator()); constexpr basic_string(initializer_list<CharT>, const Allocator& = Allocator()); constexpr basic_string(const basic_string&, const Allocator&); constexpr basic_string(basic_string&&, const Allocator&); constexpr ~basic_string(); constexpr basic_string& operator=(const basic_string& str); constexpr basic_string& operator=(basic_string&& str) noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value || allocator_traits<Allocator>::is_always_equal::value); template<class T> constexpr basic_string& operator=(const T& t)</span
缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 适用范围 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 209 | C++98 |
以下
std::basic_string
成员声明在概要说明中使用了不一致的风格:
|
统一了风格 |