find, rfind を使ってみる (C++)
C++
Published: 2019-11-23

やったこと

find rfind を使ってみます。

確認環境

$ g++ --version
g++ (Homebrew GCC 9.2.0) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

調査

test.cpp

#include <bits/stdc++.h>
using namespace std;

int main() {
    string s1 = "abcdefgabc";
    cout << s1 << endl;

    // 見つかる abc
    cout << s1.find("abc") << endl;
    cout << s1.find("abc", 3) << endl;

    // 見つからない xyz
    cout << s1.find("xyz") << endl;
    cout << string::npos << endl;

    // 後ろから検索
    cout << s1.rfind("abc") << endl;
}

出力結果

abcdefgabc
0
7
18446744073709551615
18446744073709551615
7

参考