第9.12.1章: 字符串操作参考
概述
本附录记录所有字符串特有的操作。需要某个功能时再回来查。
1. npos
::fast_io::containers::npos 定义为 ::std::numeric_limits<::std::size_t>::max()。它有两个用途:
-
“未找到” — 所有
find*操作在查找失败时返回它。 -
“直到末尾” —
subview()、substr()的默认count参数,表示“到字符串末尾”。
#include <fast_io_dsal/string.h>
#include <fast_io.h>
using ::fast_io::containers::npos;
int main() {
::fast_io::string s{"Hello, World!"};
// npos as "not found"
auto pos = s.find_character('z');
if (pos == npos) {
::fast_io::io::println("'z' not found");
}
// npos as "to end"
auto view = s.subview(7zu); // default count = npos → "World!"
}
2. 查找操作
所有查找操作都返回 size_type,找不到目标时返回 npos。
| 操作 | 说明 | 示例 |
|---|---|---|
find_character(ch, pos=0) |
从 pos 开始查找字符 ch 第一次出现的位置。 |
s.find_character('o') → 4 |
find_not_character(ch, pos=0) |
查找第一个不是 ch 的字符。 |
s.find_not_character('H') → 1 |
rfind_character(ch, pos=npos) |
从 pos 向前回溯,查找 ch 最后一次出现的位置。 |
s.rfind_character('l') → 10 |
rfind_not_character(ch, pos=npos) |
查找最后一个不是 ch 的字符。 |
s.rfind_not_character('!') → 11 |
find(view, pos=0) |
查找子串 view 第一次出现的位置。 |
s.find("World") → 7 |
rfind(view, pos=0) |
查找子串 view 最后一次出现的位置。 |
s.rfind("l") → 10 |
find_first_of(view, pos) |
查找第一个与 view 中任意字符相同的字符。 |
s.find_first_of("aeiou") → 1 ('e') |
find_first_not_of(view, pos) |
查找第一个与 view 中任何字符都不同的字符。 |
s.find_first_not_of("Helo") → 5 (',') |
find_last_of(view, pos) |
查找最后一个与 view 中任意字符相同的字符。 |
s.find_last_of("aeiou") → 8 ('o') |
find_last_not_of(view, pos) |
查找最后一个与 view 中任何字符都不同的字符。 |
s.find_last_not_of(" !") → 11 ('d') |
contains_character(ch) |
如果字符串中存在 ch,返回 true。 |
s.contains_character('W') → true |
contains(view) |
如果存在子串 view,返回 true。 |
s.contains("World") → true |
3. 子串 / 子视图操作
subview* 返回非拥有视图(string_view);substr* 返回拥有所有权的副本(string)。
| 操作 | 返回 | 说明 |
|---|---|---|
subview(pos, count=npos) |
string_view |
从 pos 开始的 count 个字符的视图。 |
subview_front(count) |
string_view |
前 count 个字符的视图。 |
subview_back(count) |
cstring_view |
后 count 个字符的视图(以空字符结尾)。 |
substr(idx, count=npos) |
string |
从 idx 开始的 count 个字符的副本。 |
substr_front(count) |
string |
前 count 个字符的副本。 |
substr_back(count) |
string |
后 count 个字符的副本。 |
所有 subview* 与 substr* 方法都有跳过边界检查的 _unchecked 变体。
4. starts_with / ends_with
| 操作 | 说明 | 示例 |
|---|---|---|
starts_with(view) |
若字符串以 view 开头则返回 true。 |
s.starts_with("Hello") → true |
starts_with_character(ch) |
若第一个字符是 ch 则返回 true。 |
s.starts_with_character('H') → true |
ends_with(view) |
若字符串以 view 结尾则返回 true。 |
s.ends_with("!") → true |
ends_with_character(ch) |
若最后一个字符是 ch 则返回 true。 |
s.ends_with_character('!') → true |
5. 修剪操作
修剪操作去掉开头和/或结尾的字符。带 _c_space 的变体修剪 C 空白字符(空格、制表符、换行等)。
| 操作 | 是否修改自身? | 说明 |
|---|---|---|
trim_c_space() |
是(返回 string&) |
就地去掉开头和结尾的空白字符。 |
trim_prefix_c_space() |
是 | 只去掉开头的空白字符。 |
trim_suffix_c_space() |
是 | 只去掉结尾的空白字符。 |
trim_subview_c_space() |
否(返回 string_view) |
返回去掉空白字符后的视图(不修改字符串)。 |
trim_prefix_subview_c_space() |
否 | 返回去掉开头空白字符后的视图。 |
trim_suffix_subview_c_space() |
否 | 返回去掉结尾空白字符后的视图。 |
此外还有通用的 trim()、trim_prefix()、trim_suffix() 重载,可接受字符类别特征来自定义字符集合。
6. 替换 / 插入 / 删除
| 操作 | 说明 |
|---|---|
replace_index(first, last, view) |
用 view 替换范围 [first, last)。 |
insert_index(idx, view) |
在下标 idx 处插入 view。 |
erase_index(idx) |
删除下标 idx 处的字符。 |
erase_index(first, last) |
删除范围 [first, last)。 |
remove_suffix(n) |
删除最后 n 个字符。 |
assign(view) |
用 view 替换整个内容。 |
assign_characters(n, ch) |
用 n 个 ch 替换内容。 |
append(view) |
把 view 追加到末尾。 |
7. 比较
字符串和字符串视图支持 == 与 <=>(C++20 三路比较)。比较按字典序进行。
::fast_io::string a{"abc"};
::fast_io::string b{"abd"};
bool eq = (a == b); // false
auto cmp = (a <=> b); // std::strong_ordering::less
8. c_str()
c_str() 返回以空字符结尾的 const char*,用于与 C API 交互。由于 fast_io::string 始终以空字符结尾,这是一次 Θ(1) 的指针返回。
9. resize_and_overwrite()
C++23 风格的方法:把字符串调整到给定大小,然后用指向原始缓冲区的指针调用用户回调。适合从 C API 或自定义逻辑填充字符串,而无需额外复制。