命名空間
變體
動作

std::basic_string_view

出自 cppreference.com
< cpp‎ | string
 
 
字串函式庫
類別
basic_string_view
(C++17)
 
 
定義於標頭檔 <string_view>
template<

    class CharT,
    class Traits = std::char_traits<CharT>

> class basic_string_view;
(自 C++17 起)

類別模板 basic_string_view 描述了一個可以參照 CharT 之常數連續序列的物件,其中序列的第一個元素位於位置 0。

對於 basic_string_view str,當某個操作導致範圍 [str.data()str.data() + str.size()) 中的指標失效時,指向 str 元素的指標、迭代器和參照也會失效。

std::basic_string_view 的每個特化都是一個 TriviallyCopyable(可平凡複製)類型。

(自 C++23 起)

典型的實作僅包含兩個成員:一個指向常數 CharT 的指標以及一個大小值。

為常見的字元型別提供了數個 typedef:

定義於標頭檔 <string_view>
類型 定義
std::string_view (C++17) std::basic_string_view<char>
std::wstring_view (C++17) std::basic_string_view<wchar_t>
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>

目錄

[編輯] 模板參數

CharT - 字元型別
Traits - CharTraits 類別用於指定對字元類型的操作。與 std::basic_string 一樣,Traits::char_type 必須與 CharT 命名相同的類型,否則程式將格式錯誤。

[編輯] 成員類型

巢狀類型 定義
traits_type Traits
value_type CharT
pointer CharT*
const_pointer const CharT*
reference CharT&
const_reference const CharT&
const_iterator 實作定義的常數,滿足 LegacyRandomAccessIterator
以及 LegacyContiguousIterator (直到 C++20)
ConstexprIterator,與 contiguous_iterator (自 C++20 起)

value_typeCharT

iterator const_iterator
const_reverse_iterator std::reverse_iterator<const_iterator>
reverse_iterator const_reverse_iterator
size_type std::size_t
difference_type std::ptrdiff_t

注意:iteratorconst_iterator 是同一類型,因為字串檢視(string view)是對常數字元序列的檢視。

所有對 Container(容器)迭代器類型的要求,同樣適用於 basic_string_viewiteratorconst_iterator 類型。

[編輯] 成員函式

建構函式與賦值
建構一個 basic_string_view
(公開成員函式)
指派一個檢視
(公開成員函式)
迭代器
回傳指向起點的反覆器
(公開成員函式)
回傳指向終點的反覆器
(公開成員函式)
回傳指向起點的反向反覆器
(公開成員函式)
回傳指向終點的反向反覆器
(公開成員函式)
元素存取
存取指定的字元
(公開成員函式)
存取指定的字元,並進行邊界檢查
(公開成員函式)
存取第一個字元
(公開成員函式)
存取最後一個字元
(公開成員函式)
回傳指向檢視中第一個字元的指標
(公開成員函式)
容量
回傳字元數量
(公開成員函式)
回傳最大可容納的字元數量
(公開成員函式)
檢查檢視是否為空
(公開成員函式)
修改器
藉由將起始位置向前移動來縮減檢視
(公開成員函式)
藉由將結束位置向後移動來縮減檢視
(公開成員函式)
交換內容
(公開成員函式)
作業 (Operations)
複製字元
(公開成員函式)
回傳子字串
(公開成員函式)
比較兩個檢視
(公開成員函式)
檢查字串檢視是否以給定的前綴開頭
(公開成員函式)
(C++20)
檢查字串檢視是否以給定的後綴結尾
(公開成員函式)
(C++23)
檢查字串檢視是否包含給定的子字串或字元
(公開成員函式)
在檢視中尋找字元
(公開成員函式)
尋找子字串的最後一次出現
(公開成員函式)
尋找字元的首次出現
(公開成員函式)
尋找字元的最後一次出現
(公開成員函式)
尋找字元的首次不匹配
(公開成員函式)
尋找字元的最後一次不匹配
(公開成員函式)

常數

[靜態]
特殊值。確切含義取決於上下文
(公開靜態成員常數)

[編輯] 非成員函式

(C++17)(於 C++20 移除)(於 C++20 移除)(於 C++20 移除)(於 C++20 移除)(於 C++20 移除)(C++20)
按字典順序比較兩個字串檢視
(函式模板)
輸入/輸出
對字串檢視執行串流輸出
(函式模板)

[編輯] 字面量

定義於內聯命名空間 std::literals::string_view_literals
建立字元陣列字面值的字串視圖
(函式) [編輯]

[編輯] 輔助類別

字串檢視 (string views) 的雜湊支援
(類別樣板特例化) [編輯]

[編輯] 輔助模板

template< class CharT, class Traits >

inline constexpr bool

    ranges::enable_borrowed_range<std::basic_string_view<CharT, Traits>> = true;
(自 C++20 起)

此對 ranges::enable_borrowed_range 的特化使 basic_string_view 滿足 borrowed_range

template< class CharT, class Traits >

inline constexpr bool

    ranges::enable_view<std::basic_string_view<CharT, Traits>> = true;
(自 C++20 起)

此對 ranges::enable_view 的特化使 basic_string_view 滿足 view

推導指引

(自 C++20 起)

[編輯] 附註

確保 std::string_view 的生命週期不超過其所指向的字元陣列,是程式設計師的責任。

std::string_view good{"a string literal"};
    // "Good" case: `good` points to a static array.
    // String literals reside in persistent data storage.
 
std::string_view bad{"a temporary string"s};
    // "Bad" case: `bad` holds a dangling pointer since the std::string temporary,
    // created by std::operator""s, will be destroyed at the end of the statement.

std::basic_string_view 的特化在所有現有的實作中,即使在 C++23 引入正式要求之前,已經是可平凡複製(trivially copyable)的類型。

功能測試巨集 數值 標準 功能
__cpp_lib_string_view 201606L (C++17) std::string_view
201803L (C++20) Constexpr 迭代器 (ConstexprIterator)
__cpp_lib_string_contains 202011L (C++23) contains

[編輯] 範例

#include <iostream>
#include <string_view>
 
int main()
{
    constexpr std::string_view unicode[]{"▀▄─", "▄▀─", "▀─▄", "▄─▀"};
 
    for (int y{}, p{}; y != 6; ++y, p = ((p + 1) % 4))
    {
        for (int x{}; x != 16; ++x)
            std::cout << unicode[p];
        std::cout << '\n';
    }
}

輸出

▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─
▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─
▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄
▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀
▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─
▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯應用於之前的 C++ 標準。

DR 應用於 出版時的行為 正確的行為
LWG 3203 C++17 僅指標、迭代器與參照
由成員函式回傳之
basic_string_view 可能會失效
所有指標、迭代器與參照
指向 basic_string_view 的元素
可能會失效

[編輯] 參見

儲存並操作字元序列
(類別模板) [編輯]
連接兩個字串、一個字串與一個 char,或一個字串與 string_view
(函式模板) [編輯]
(C++20)
連續物件序列上的非擁有型視圖
(類別樣板) [編輯]
引用在 列表初始化 中建立的暫存陣列
(類別模板) [編輯]
English Deutsch 日本語 中文(简体) 中文(繁體)