Namespaces
Variants

std:: strpbrk

From cppreference.net
定义于头文件 <cstring>
const char * strpbrk ( const char * dest, const char * breakset ) ;
char * strpbrk ( char * dest, const char * breakset ) ;

扫描由 dest 指向的以空字符结尾的字节字符串,查找由 breakset 指向的以空字符结尾的字节字符串中的任意字符,并返回指向该字符的指针。

目录

参数

dest - 指向待分析的空终止字节字符串的指针
breakset - 指向包含待搜索字符的空终止字节字符串的指针

返回值

指向 dest 中首个同时存在于 breakset 中的字符的指针,若不存在此类字符则返回空指针。

注释

该名称代表“字符串指针中断”,因为它返回指向第一个分隔符(“中断”)字符的指针。

示例

#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 << "There are " << cnt << " words\n";
}

输出:

" world, friend of mine!"
", friend of mine!"
" of mine!"
" mine!"
"!"
There are 5 words

参见

返回仅包含不在另一个字节字符串中的字符的最大起始段长度
(函数)
查找字节字符串中的下一个标记
(函数)
查找字符的首次出现位置
(函数)
在一个宽字符串中查找另一个宽字符串中任意宽字符的首次出现位置
(函数)
C 文档 for strpbrk