标准库标头 <string_view> (C++17)

来自cppreference.com
< cpp‎ | header


 
 
标准库头
 

此头文件是字符串库的一部分。

目录

包含

(C++20)
三路比较运算符支持[编辑]

只读的字符串视图
(类模板)[编辑]
std::string_view(C++17)std::basic_string_view<char>
std::u8string_view(C++20)std::basic_string_view<char8_t>
std::u16string_view(C++17)std::basic_string_view<char16_t>
std::u32string_view(C++17)std::basic_string_view<char32_t>
std::wstring_view(C++17)std::basic_string_view<wchar_t>
字符串视图的散列支持
(类模板特化)[编辑]
前置声明
在标头 <functional> 定义
(C++11)
散列函数对象
(类模板)[编辑]

函数

(C++20 移除)(C++20 移除)(C++20 移除)(C++20 移除)(C++20 移除)(C++20)
以字典序比较两个字符串视图
(函数模板)[编辑]
(C++17)
进行字符串视图的流输出
(函数模板)[编辑]
交换两个对象的值
(函数模板)[编辑]
范围访问
(C++11)(C++14)
返回指向容器或数组起始的迭代器
(函数模板)[编辑]
(C++11)(C++14)
返回指向容器或数组结尾的迭代器
(函数模板)[编辑]
返回指向一个容器或数组的逆向迭代器
(函数模板)[编辑]
(C++14)
返回容器或数组的逆向尾迭代器
(函数模板)[编辑]
(C++17)(C++20)
返回容器或数组的大小
(函数模板)[编辑]
(C++17)
检查容器是否为空
(函数模板)[编辑]
(C++17)
获得指向底层数组的指针
(函数模板)[编辑]
字面量
在内联命名空间 std::literals::string_view_literals 定义
创建一个字符数组字面量的字符串视图
(函数)[编辑]

[编辑]概要

#include <compare>   namespace std {// 类模板 basic_­string_­viewtemplate<class CharT, class Traits = char_traits<CharT>>class basic_string_view;   template<class CharT, class Traits>inlineconstexprboolranges::enable_view<basic_string_view<CharT, Traits>>=true;template<class CharT, class Traits>inlineconstexprboolranges::enable_borrowed_range<basic_string_view<CharT, Traits>>=true;   // 非成员函数template<class CharT, class Traits>constexprbool operator==(basic_string_view<CharT, Traits> x, basic_string_view<CharT, Traits> y)noexcept;template<class CharT, class Traits>constexpr/* 见描述 */ operator<=>(basic_string_view<CharT, Traits> x, basic_string_view<CharT, Traits> y)noexcept;   // 充足的比较函数额外重载   // 插入器与提取器template<class CharT, class Traits> basic_ostream<CharT, Traits>& operator<<(basic_ostream<CharT, Traits>& os, basic_string_view<CharT, Traits> str);   // basic_­string_­view typedef 名using string_view = basic_string_view<char>;using u8string_view = basic_string_view<char8_t>;using u16string_view = basic_string_view<char16_t>;using u32string_view = basic_string_view<char32_t>;using wstring_view = basic_string_view<wchar_t>;   // 散列支持template<class T>struct hash;template<>struct hash<string_view>;template<>struct hash<u8string_view>;template<>struct hash<u16string_view>;template<>struct hash<u32string_view>;template<>struct hash<wstring_view>;   inlinenamespace literals {inlinenamespace string_view_literals {// basic_­string_­view 字面量的后缀constexpr string_view operator""sv(constchar* str, size_t len)noexcept;constexpr u8string_view operator""sv(const char8_t* str, size_t len)noexcept;constexpr u16string_view operator""sv(constchar16_t* str, size_t len)noexcept;constexpr u32string_view operator""sv(constchar32_t* str, size_t len)noexcept;constexpr wstring_view operator""sv(constwchar_t* str, size_t len)noexcept;}}}

[编辑]类模板 std::basic_string_view

namespace std {template<class CharT, class Traits = char_traits<CharT>>class basic_string_view {public:// 类型using Traits_type = Traits;using value_type = CharT;using pointer = value_type*;using const_pointer =const value_type*;using reference = value_type&;using const_reference =const value_type&;using const_iterator =/* 由实现定义 */using iterator = const_iterator;using const_reverse_iterator = reverse_iterator<const_iterator>;using reverse_iterator = const_reverse_iterator;using size_type = size_t;using difference_type = ptrdiff_t;staticconstexpr size_type npos = size_type(-1);   // 构造与赋值constexpr basic_string_view()noexcept;constexpr basic_string_view(const basic_string_view&)noexcept=default;constexpr basic_string_view& operator=(const basic_string_view&)noexcept=default;constexpr basic_string_view(const CharT* str);constexpr basic_string_view(nullptr_t)= delete;constexpr basic_string_view(const CharT* str, size_type len);template<class It, class End>constexpr basic_string_view(It begin, End end);template<class R>constexprexplicit basic_string_view(R&& r);   // 迭代器支持constexpr const_iterator begin()constnoexcept;constexpr const_iterator end()constnoexcept;constexpr const_iterator cbegin()constnoexcept;constexpr const_iterator cend()constnoexcept;constexpr const_reverse_iterator rbegin()constnoexcept;constexpr const_reverse_iterator rend()constnoexcept;constexpr const_reverse_iterator crbegin()constnoexcept;constexpr const_reverse_iterator crend()constnoexcept;   // 容量constexpr size_type size()constnoexcept;constexpr size_type length()constnoexcept;constexpr size_type max_size()constnoexcept;constexprbool empty()constnoexcept;   // 元素访问constexpr const_reference operator[](size_type pos)const;constexpr const_reference at(size_type pos)const;constexpr const_reference front()const;constexpr const_reference back()const;constexpr const_pointer data()constnoexcept;   // 修改器constexprvoid remove_prefix(size_type n);constexprvoid remove_suffix(size_type n);constexprvoid swap(basic_string_view& s)noexcept;   // 字符串操作constexpr size_type copy(CharT* s, size_type n, size_type pos =0)const;   constexpr basic_string_view substr(size_type pos =0, size_type n = npos)const;   constexprint compare(basic_string_view s)constnoexcept;constexprint compare(size_type pos1, size_type n1, basic_string_view s)const;constexprint compare(size_type pos1, size_type n1, basic_string_view s, size_type pos2, size_type n2)const;constexprint compare(const CharT* s)const;constexprint compare(size_type pos1, size_type n1, const CharT* s)const;constexprint compare(size_type pos1, size_type n1, const CharT* s, size_type n2)const;   constexprbool starts_with(basic_string_view x)constnoexcept;constexprbool starts_with(CharT x)constnoexcept;constexprbool starts_with(const CharT* x)const;constexprbool ends_with(basic_string_view x)constnoexcept;constexprbool ends_with(CharT x)constnoexcept;constexprbool ends_with(const CharT* x)const;   constexprbool contains(basic_string_view x)constnoexcept;constexprbool contains(CharT x)constnoexcept;constexprbool contains(const CharT* x)const;   // 搜索constexpr size_type find(basic_string_view s, size_type pos =0)constnoexcept;constexpr size_type find(CharT c, size_type pos =0)constnoexcept;constexpr size_type find(const CharT* s, size_type pos, size_type n)const;constexpr size_type find(const CharT* s, size_type pos =0)const;constexpr size_type rfind(basic_string_view s, size_type pos = npos)constnoexcept;constexpr size_type rfind(CharT c, size_type pos = npos)constnoexcept;constexpr size_type rfind(const CharT* s, size_type pos, size_type n)const;constexpr size_type rfind(const CharT* s, size_type pos = npos)const;   constexpr size_type find_first_of(basic_string_view s, size_type pos =0)constnoexcept;constexpr size_type find_first_of(CharT c, size_type pos =0)constnoexcept;constexpr size_type find_first_of(const CharT* s, size_type pos, size_type n)const;constexpr size_type find_first_of(const CharT* s, size_type pos =0)const;constexpr size_type find_last_of(basic_string_view s, size_type pos = npos)constnoexcept;constexpr size_type find_last_of(CharT c, size_type pos = npos)constnoexcept;constexpr size_type find_last_of(const CharT* s, size_type pos, size_type n)const;constexpr size_type find_last_of(const CharT* s, size_type pos = npos)const;constexpr size_type find_first_not_of(basic_string_view s, size_type pos =0)constnoexcept;constexpr size_type find_first_not_of(CharT c, size_type pos =0)constnoexcept;constexpr size_type find_first_not_of(const CharT* s, size_type pos, size_type n)const;constexpr size_type find_first_not_of(const CharT* s, size_type pos =0)const;constexpr size_type find_last_not_of(basic_string_view s, size_type pos = npos)constnoexcept;constexpr size_type find_last_not_of(CharT c, size_type pos = npos)constnoexcept;constexpr size_type find_last_not_of(const CharT* s, size_type pos, size_type n)const;constexpr size_type find_last_not_of(const CharT* s, size_type pos = npos)const;   private: const_pointer data_;// 仅用于阐释 size_type size_;// 仅用于阐释};   // 推导指引template<class It, class End> basic_string_view(It, End)-> basic_string_view<iter_value_t<It>>;   template<class R> basic_string_view(R&&)-> basic_string_view<ranges::range_value_t<R>>;}
close