namespace std {
const char* strpbrk(const char* s1, const char* s2); // (1)
char* strpbrk(char* s1, const char* s2); // (2)
}
概要
文字列から、指定した文字集合に含まれるいずれかの文字が現れる位置を求める。
- (1) : 引数
s1がconst修飾されている場合に、const修飾を保持して結果を返すオーバーロード。 - (2) : 引数
s1がconst修飾されていない場合に、非constのポインタを返すオーバーロード。
効果
s1が指す文字列から、s2が指す文字列に含まれるいずれかの文字が最初に現れる位置を検索する。
戻り値
見つかった場合、その位置を指すポインタを返す。見つからなかった場合、ヌルポインタを返す。
- (1) :
const char*を返す。 - (2) :
char*を返す。
備考
- この関数は、フリースタンディング処理系でも使用できる。
- C言語の
strpbrkはchar*を返す単一の関数だが、C++ではconst修飾を保持するために2つのオーバーロードが提供される。
例
#include <cstring>
#include <iostream>
int main()
{
const char s[] = "hello world";
// 'o'または' 'が最初に現れる位置を検索する
const char* p = std::strpbrk(s, "o ");
std::cout << (p != nullptr ? p - s : -1) << std::endl;
}
出力
4
バージョン
言語
- C++98
関連項目
参照
- P2338R4 Freestanding Library: Character primitives and the C library
- C++26で、この関数がフリースタンディング処理系で使用可能になった