最終更新日時:
が更新

履歴 編集

function
<cstring>

std::strpbrk

namespace std {
  const char* strpbrk(const char* s1, const char* s2); // (1)
  char* strpbrk(char* s1, const char* s2);             // (2)
}

概要

文字列から、指定した文字集合に含まれるいずれかの文字が現れる位置を求める。

  • (1) : 引数s1const修飾されている場合に、const修飾を保持して結果を返すオーバーロード
  • (2) : 引数s1const修飾されていない場合に、非constのポインタを返すオーバーロード

効果

s1が指す文字列から、s2が指す文字列に含まれるいずれかの文字が最初に現れる位置を検索する。

戻り値

見つかった場合、その位置を指すポインタを返す。見つからなかった場合、ヌルポインタを返す。

  • (1) : const char*を返す。
  • (2) : char*を返す。

備考

  • この関数は、フリースタンディング処理系でも使用できる。
  • C言語のstrpbrkchar*を返す単一の関数だが、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

関連項目

  • strcspn: 指定した文字が現れるまでの長さを求める
  • strchr: 文字を検索する

参照