std::strpbrk
出自cppreference.com
| 在標頭 <cstring> 定義
|
||
| |
||
| |
||
在 dest 所指向的空終止字節串中,掃描來自 breakset 所指向的空終止字節串的任何字符,並返回指向該字符的指針。
參數
| dest | - | 指向要分析的空終止字節字符串的指針 |
| breakset | - | 指向含要搜索的字符的空終止字節字符串的指針 |
返回值
指向 dest 中首個亦在 breakset 中的字符的指針,或若這種字符不存在則為空指針。
註解
名稱代表「字符串指針打斷 (string pointer break)」,因為它返回指向首個分隔符(「打斷」)的指針。
示例
運行此代碼
#include <cstring>
#include <iomanip>
#include <iostream>
int main()
{
const char* str = "hello world, friend of mine!";
const char* sep = " ,!";
unsigned int cnt = 0;
do
{
str = std::strpbrk(str, sep); // 寻找分隔符
std::cout << std::quoted(str) << '\n';
if (str)
str += std::strspn(str, sep); // 跳过分隔符
++cnt; // 增加词计数
} while (str && *str);
std::cout << "有 " << cnt << " 个单词\n";
}
輸出:
" world, friend of mine!"
", friend of mine!"
" of mine!"
" mine!"
"!"
有 5 个单词
參閱
| 返回僅由另一字節字符串中找不到的字符組成的最大起始段的長度 (函數) | |
| 尋找字節字符串中的下個記號 (函數) | |
| 尋找字符的首次出現 (函數) | |
| 在寬字符串中,尋找另一寬字符串中任何字符的首個位置 (函數) | |
strpbrk 的 C 文檔
| |