名稱空間
變體
操作

std::basic_string

來自 cppreference.com
< cpp‎ | string
 
 
 
std::basic_string
 
定義於標頭檔案 <string>
template<

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

> class basic_string;
(1)
namespace pmr {

template<
    class CharT,
    class Traits = std::char_traits<CharT>
> using basic_string =
    std::basic_string<CharT, Traits, std::pmr::polymorphic_allocator<CharT>>;

}
(2) (C++17 起)

類模板 basic_string 儲存和操作字元類物件序列,這些物件是非陣列的 字元型別,為 TrivialTypeStandardLayoutType。此型別不依賴於字元型別,也不依賴於該型別上的操作性質。操作的定義由 `Traits` 模板引數提供——一個 `std::char_traits` 的特化或相容的 traits 類。

basic_string 的元素是連續儲存的,也就是說,對於 basic_string s,對於 [0, s.size()) 範圍內的任何 n,都有 &*(s.begin() + n) == &*s.begin() + n,並且 *(s.begin() + s.size()) 的值為 CharT() (空終止符)(C++11 起);或者,等效地,指向 s[0] 的指標可以傳遞給期望指向 CharT 陣列(直至 C++11)空終止陣列(C++11 起) 首個元素的函式。

std::basic_string 滿足 AllocatorAwareContainer 的要求(除了自定義的 construct/destroy 不用於元素的構造/析構),SequenceContainerContiguousContainer(C++17 起) 的要求。

如果 `Traits::char_type` 和 `Allocator::char_type` 中的任何一個與 `CharT` 不同,則程式是非良構的。

`std::basic_string` 的所有成員函式都是 constexpr:可以在常量表達式求值中建立和使用 `std::basic_string` 物件。

然而,`std::basic_string` 物件通常不能是 constexpr,因為任何動態分配的儲存都必須在同一常量表達式的求值中釋放。

(C++20 起)

提供了用於常見字元型別的幾個 typedef

定義於標頭檔案 <string>
型別 定義
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>

目錄

[編輯] 模板引數

CharT - 字元型別
特性 - 指定字元型別操作的 trait 類
Allocator - 用於分配內部儲存的 Allocator 型別

[編輯] 巢狀型別

型別 定義
traits_type 特性
value_type CharT
allocator_type Allocator[編輯]
size_type
Allocator::size_type (C++11 前)
std::allocator_traits<Allocator>::size_type (C++11 起)
[編輯]
difference_type
Allocator::difference_type (C++11 前)
std::allocator_traits<Allocator>::difference_type (C++11 起)
[編輯]
reference value_type&[編輯]
const_reference const value_type&[編輯]
pointer

Allocator::pointer

(C++11 前)

std::allocator_traits<Allocator>::pointer

(C++11 起)
[編輯]
const_pointer

Allocator::const_pointer

(C++11 前)

std::allocator_traits<Allocator>::const_pointer

(C++11 起)
[編輯]
iterator

LegacyRandomAccessIteratorLegacyContiguousIteratorvalue_type

(C++20 前)

LegacyRandomAccessIteratorcontiguous_iteratorConstexprIteratorvalue_type

(C++20 起)
[編輯]
const_iterator

LegacyRandomAccessIteratorLegacyContiguousIteratorconst value_type

(C++20 前)

LegacyRandomAccessIteratorcontiguous_iteratorConstexprIteratorconst value_type

(C++20 起)
[編輯]
reverse_iterator std::reverse_iterator<iterator>[編輯]
const_reverse_iterator std::reverse_iterator<const_iterator>[編輯]

[編輯] 資料成員

constexpr size_type npos [static] 特殊值 size_type(-1),其確切含義取決於上下文

[編輯] 成員函式

構造一個 basic_string
(public 成員函式) [編輯]
銷燬字串,如果使用了內部儲存則釋放它
(public 成員函式) [編輯]
給字串賦值
(public 成員函式) [編輯]
給字串賦值字元
(public 成員函式) [編輯]
給字串賦值一個字元範圍
(public 成員函式) [編輯]
返回關聯的分配器
(public 成員函式) [編輯]
元素訪問
帶邊界檢查訪問指定字元
(public 成員函式) [編輯]
訪問指定的字元
(public 成員函式) [編輯]
(DR*)
訪問第一個字元
(public 成員函式) [編輯]
(DR*)
訪問最後一個字元
(public 成員函式) [編輯]
返回指向字串第一個字元的指標
(public 成員函式) [編輯]
返回字串的不可修改的標準C字元陣列版本
(public 成員函式) [編輯]
返回一個指向整個字串的不可修改的 basic_string_view
(public 成員函式) [編輯]
迭代器
返回指向起始的迭代器
(public 成員函式) [編輯]
(C++11)
返回指向末尾的迭代器
(public 成員函式) [編輯]
返回指向起始的逆向迭代器
(public 成員函式) [編輯]
(C++11)
返回指向末尾的逆向迭代器
(public 成員函式) [編輯]
容量
檢查字串是否為空
(public 成員函式) [編輯]
返回字元數
(public 成員函式) [編輯]
返回字元的最大數量
(public 成員函式) [編輯]
預留儲存空間
(public 成員函式) [編輯]
返回當前分配儲存中可容納的字元數
(public 成員函式) [編輯]
透過釋放未使用的記憶體來減少記憶體使用
(public 成員函式) [編輯]
修改器
清除內容
(public 成員函式) [編輯]
插入字元
(public 成員函式) [編輯]
插入一個字元範圍
(public 成員函式) [編輯]
移除字元
(public 成員函式) [編輯]
在末尾新增字元
(public 成員函式) [編輯]
移除最後一個字元
(public 成員函式) [編輯]
將字元追加到末尾
(public 成員函式) [編輯]
在末尾追加一個字元範圍
(public 成員函式) [編輯]
將字元追加到末尾
(public 成員函式) [編輯]
替換字串的指定部分
(public 成員函式) [編輯]
用一個字元範圍替換字串的指定部分
(public 成員函式) [編輯]
複製字元
(public 成員函式) [編輯]
更改儲存的字元數
(public 成員函式) [編輯]
改變儲存的字元數量,並可能透過使用者提供的操作覆蓋不確定的內容
(public 成員函式) [編輯]
交換內容
(public 成員函式) [編輯]
搜尋
查詢給定子字串的第一次出現
(public 成員函式) [編輯]
查詢子串的最後一次出現
(public 成員函式) [編輯]
查詢字元的首次出現
(public 成員函式) [編輯]
查詢字元的首次缺席
(public 成員函式) [編輯]
查詢字元的最後一次出現
(public 成員函式) [編輯]
查詢字元的最後一次缺席
(public 成員函式) [編輯]
操作
比較兩個字串
(public 成員函式) [編輯]
檢查字串是否以給定字首開頭
(public 成員函式) [編輯]
(C++20)
檢查字串是否以給定字尾結尾
(public 成員函式) [編輯]
(C++23)
檢查字串是否包含給定子字串或字元
(public 成員函式) [編輯]
返回子字串
(public 成員函式) [編輯]

[編輯] 非成員函式

連線兩個字串,一個字串和一個 char,或者一個字串和一個 string_view
(函式模板) [編輯]
(C++20 中移除)(C++20 中移除)(C++20 中移除)(C++20 中移除)(C++20 中移除)(C++20 起)
按字典序比較兩個字串
(函式模板) [編輯]
特化 std::swap 演算法
(函式模板) [編輯]
擦除所有滿足特定標準的元素
(函式模板) [編輯]
輸入/輸出
對字串執行流輸入和輸出
(函式模板) [編輯]
從 I/O 流讀取資料到字串
(函式模板) [編輯]
數值轉換
(C++11)(C++11)(C++11)
將字串轉換為有符號整數
(函式) [編輯]
(C++11 起)(C++11 起)
將字串轉換為無符號整數
(函式) [編輯]
(C++11)(C++11)(C++11)
將字串轉換為浮點值
(函式) [編輯]
(C++11)
將整數或浮點值轉換為 string
(函式) [編輯]
將整數或浮點值轉換為 wstring
(函式) [編輯]

[編輯] 字面量

在內聯名稱空間 std::literals::string_literals 中定義
將字元陣列字面量轉換為 basic_string
(函式) [編輯]

[編輯] 輔助類

字串的雜湊支援
(類模板特化) [編輯]

[編輯] 推導指南 (C++17 起)

[編輯] 迭代器失效

引用、指標和迭代器指向 basic_string 元素的有效性可能會因任何接受非 const basic_string 引用作為引數的標準庫函式而失效,例如 std::getlinestd::swapoperator>>,以及呼叫非 const 成員函式,除了 operator[]atdatafrontbackbeginrbeginendrend

[編輯]

儘管在 C++23 之前,構造或銷燬 `std::basic_string` 的元素時需要使用自定義的 `construct` 或 `destroy`,但所有實現都只使用了預設機制。此要求已透過 P1072R10 糾正,以符合現有實踐。

特性測試 標準 特性
__cpp_lib_string_udls 201304L (C++14) 字串型別的使用者定義字面量
__cpp_lib_starts_ends_with 201711L (C++20) starts_with, ends_with
__cpp_lib_constexpr_string 201907L (C++20) std::basic_string 的 Constexpr
__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) 接受容器兼容範圍的構造、插入和替換成員函式

[編輯] 示例

#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';
}

輸出

hello world
world hello
w
World

[編輯] 缺陷報告

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

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 530 C++98 basic_string 元素儲存的連續性
意外地被 LWG259 設定為非必需
再次要求
LWG 2861 C++98 value_typeTraits::char_type 改為 CharT
LWG 2994
(P1148R0)
C++98 如果 Traits::char_type[1] 中的任何一個
Allocator::char_typeCharT 不同,則行為未定義
程式是
非良構的
  1. Traits::char_type 的情況已在 P1148R0 中修復。

[編輯] 另請參閱

只讀字串檢視
(類模板) [編輯]

[編輯] 外部連結

C++ 字串處理