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 内にも存在するもの、またはそのような文字が存在しない場合はヌルポインタ。

注記

この名前は「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 << "There are " << cnt << " words\n";
}

出力:

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

関連項目

他のバイト文字列に見つからない文字のみで構成される
最大の先頭セグメントの長さを返す
(関数)
バイト文字列内の次のトークンを検索する
(関数)
文字の最初の出現を検索する
(関数)
ワイド文字列内の任意のワイド文字の最初の位置を別のワイド文字列で検索する
(関数)