名前空間
変種
操作

std::basic_string<CharT,Traits,Allocator>::find

From cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
size_type find( const basic_string& str, size_type pos = 0 ) const;
(1) (C++11 以降 noexcept)
(C++20 以降 constexpr)
size_type find( const CharT* s, size_type pos, size_type count ) const;
(2) (C++20 以降 constexpr)
size_type find( const CharT* s, size_type pos = 0 ) const;
(3) (C++20 以降 constexpr)
size_type find( CharT ch, size_type pos = 0 ) const;
(4) (C++11 以降 noexcept)
(C++20 以降 constexpr)
template< class StringViewLike >

size_type find( const StringViewLike& t,

                size_type pos = 0 ) const noexcept(/* see below */);
(5) (C++17以降)
(C++20 以降 constexpr)

指定された文字シーケンスと等しい最初の部分文字列を検索します。検索は pos から開始されます。つまり、見つかった部分文字列は pos より前の位置で見つかってはなりません。

1) str と等しい最初の部分文字列を検索します。
2) 範囲 [ss + count) と等しい最初の部分文字列を検索します。この範囲にはヌル文字が含まれる場合があります。
範囲[ss + count)有効な範囲でない場合、動作は未定義です。
3) s が指す文字文字列と等しい最初の部分文字列を検索します。文字列の長さは、最初のヌル文字を使用して Traits::length(s) によって決定されます。
範囲[ss + Traits::length(s))有効な範囲でない場合、動作は未定義です。
4) 文字 ch を検索します(正式なルールでは単一文字の部分文字列として扱われます)。
5) t を、あたかも std::basic_string_view<CharT, Traits> sv = t; のように文字列ビュー sv に暗黙的に変換し、その sv と等しい最初の部分文字列を検索します。
このオーバーロードは、std::is_convertible_v<const StringViewLike&,
                      std::basic_string_view<CharT, Traits>>
true であり、かつ std::is_convertible_v<const StringViewLike&, const CharT*>false の場合にのみ、オーバーロード解決に参加します。

形式的には、部分文字列 str は、以下のすべてが true である場合に、位置 xpos見つかったと言います。

  • xpos >= pos
  • xpos + str.size() <= size()
  • str のすべての位置 n について、Traits::eq(at(xpos + n), str.at(n))

特に、これは以下を意味します。

  • pos <= size() - str.size() でない限り、部分文字列は見つかりません。
  • pos <= size() である場合、空の部分文字列は pos で見つかります。
  • 空でない部分文字列の場合、pos >= size() であれば、関数は常に npos を返します。

目次

[編集] パラメータ

str - 検索する文字列
pos - 検索を開始する位置
count - 検索する部分文字列の長さ
s - 検索するC文字列へのポインタ
文字 - 検索する文字
t - 検索するオブジェクト(std::basic_string_view に変換可能)

[編集] 戻り値

見つかった部分文字列の最初の文字の位置、またはそのような部分文字列が見つからなかった場合は npos

[編集] 例外

1,4) 何も投げません。
5)
noexcept 指定:  
noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)

何らかの理由で例外がスローされた場合、この関数は効果がありません(強力な例外安全保証)。

[編集]

#include <iomanip>
#include <iostream>
#include <string>
 
void print(int id, std::string::size_type n, std::string const& s)
{
    std::cout << id << ") ";
    if (std::string::npos == n)
        std::cout << "not found! n == npos\n";
    else
        std::cout << "found @ n = " << n << ", substr(" << n << ") = "
                  << std::quoted(s.substr(n)) << '\n';
}
 
int main()
{
    std::string::size_type n;
    std::string const s = "This is a string"; /*
                             ^  ^  ^
                             1  2  3          */
 
    // search from beginning of string
    n = s.find("is");
    print(1, n, s);
 
    // search from position 5
    n = s.find("is", 5);
    print(2, n, s);
 
    // find a single character
    n = s.find('a');
    print(3, n, s);
 
    // find a single character
    n = s.find('q');
    print(4, n, s);
}

出力

1) found @ n = 2, substr(2) = "is is a string"
2) found @ n = 5, substr(5) = "is a string"
3) found @ n = 8, substr(8) = "a string"
4) not found! n == npos

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 847 C++98 例外安全性保証がなかった 強力な例外安全性保証を追加
LWG 2064 C++11 (3,4) のオーバーロードは noexcept でした。 削除
LWG 2946 C++17 (5) のオーバーロードは、一部のケースで曖昧さを引き起こしました。 テンプレートにすることで回避されました。
P1148R0 C++11
C++17
(4,5) のオーバーロードの noexcept は
LWG2064/LWG2946 によって誤って削除されました。
復元されました。

[編集] 関連項目

部分文字列が最初に出現する箇所を見つける
(関数) [編集]
あるワイド文字列内で、別のワイド文字列が最初に現れる場所を見つける
(関数) [編集]
最初に出現する文字を見つける
(関数) [編集]
ワイド文字列内でワイド文字が最初に現れる場所を見つける
(関数) [編集]
部分文字列が最後に現れる位置を見つける
(public member function) [編集]
文字が最初に現れる位置を見つける
(public member function) [編集]
文字が最初に現れない位置を見つける
(public member function) [編集]
文字が最後に現れる位置を見つける
(public member function) [編集]
文字が最後に現れない位置を見つける
(public member function) [編集]
ビュー内の文字を検索する
(std::basic_string_view<CharT,Traits> の public メンバ関数) [編集]
要素の範囲の最初の出現を検索する
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)