GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::strstr - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. cstring
  3. strstr

最終更新日時: 2026年08月18日 02時34分25秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<cstring>

std::strstr

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

概要

文字列から部分文字列を検索する。

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

効果

s1が指す文字列から、s2が指す文字列 (終端のヌル文字を含まない) が部分文字列として最初に現れる位置を検索する。

戻り値

見つかった場合、その位置を指すポインタを返す。見つからなかった場合、ヌルポインタを返す。s2が空文字列を指す場合はs1を返す。

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

備考

  • この関数は、フリースタンディング処理系でも使用できる。
  • C言語のstrstrはchar*を返す単一の関数だが、C++ではconst修飾を保持するために2つのオーバーロードが提供される。

例

#include <cstring>
#include <iostream>

int main()
{
  const char s[] = "hello world";

  const char* p = std::strstr(s, "world");

  std::cout << (p != nullptr ? p - s : -1) << std::endl;
}

出力

6

バージョン

言語

  • C++98

関連項目

  • strchr: 文字を検索する
  • strrchr: 文字を後ろから検索する

参照

  • P2338R4 Freestanding Library: Character primitives and the C library
    • C++26で、この関数がフリースタンディング処理系で使用可能になった
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.