size_type
find(const basic_string& str,
size_type pos = 0) const; // (1) C++03
size_type
find(const basic_string& str,
size_type pos = 0) const noexcept; // (1) C++11
constexpr size_type
find(const basic_string& str,
size_type pos = 0) const noexcept; // (1) C++20
size_type
find(const charT* s,
size_type pos,
size_type n) const; // (2) C++03
constexpr size_type
find(const charT* s,
size_type pos,
size_type n) const; // (2) C++20
size_type
find(const charT* s,
size_type pos = 0) const; // (3) C++03
constexpr size_type
find(const charT* s,
size_type pos = 0) const; // (3) C++20
size_type
find(charT c,
size_type pos = 0) const; // (4) C++03
constexpr size_type
find(charT c,
size_type pos = 0) const; // (4) C++20
// string_viewを引数に取るオーバーロード
template <class T>
size_type
find(const T& t,
size_type pos = 0) const noexcept(下記参照); // (5) C++17
template <class T>
constexpr size_type
find(const T& t,
size_type pos = 0) const noexcept(下記参照); // (5) C++20
概要
指定した文字列を検索する。
テンプレートパラメータ制約
- (5) :
is_convertible_v<const T&, basic_string_view<charT, traits>>がtrueであることis_convertible_v<const T&, const charT*>がfalseであること
事前条件
- (3) :
sは少なくともtraits_type::length(s) + 1の要素を持つcharTの配列を指していること。
効果
- (1) :
pos以降で最初にstrと一致する位置を返す。 - (2) :
pos以降で最初にsと一致する位置を返す。sは長さnの文字列へのポインタである。 - (3) : (2) と同様だが、こちらは NULL 終端の文字列を扱う。
- (4) :
pos以降で最初にcと一致する位置を返す。 - (5) :
basic_string_view<charT, traits> sv = t;として変数svを作成し、pos以降で最初にsvと一致する位置を返す。
戻り値
見つかればその位置を返し、見つからない場合は basic_string::npos を返す。
例外
- (1) : 投げない
- (5) :
noexcept内の式は、以下と等価である
is_nothrow_convertible_v<const T&, basic_string_view<charT, traits>>
備考
- 文字列の一致は、文字列の各文字について
traits_type::eq()を用いて検査される。
例えば、(1) の形式の場合、以下のような条件を満たす最小のxposを求める。pos <= xposかつxpos + str.size() <= size()0 <= IかつI < str.size()を満たす全てのIについて、traits_type::eq(at(xpos + I), str.at(I))
- (3) の形式の場合、
sの文字列長はtraits_type::length(s)で求められる。
例
#include <iostream>
#include <string>
int main()
{
const std::string s("hello, world. welcome to C++ world.");
const std::string find_word("world");
std::string::size_type pos = s.find(find_word);
while (pos != std::string::npos) {
std::cout << pos << std::endl;
pos = s.find(find_word, pos + find_word.length());
}
}
出力
7
29
実装例
(1) 以外の形式は、(1) の形式を使用して実装することができる。
// (2)
template <class charT, class traits, class Allocator>
size_type basic_string<charT, traits, Allocator>::find(const charT* s, size_type pos, size_type n) const
{
return find(std::basic_string(s, n), pos);
}
// (3)
template <class charT, class traits, class Allocator>
size_type basic_string<charT, traits, Allocator>::find(const charT* s, size_type pos = 0) const
{
return find(std::basic_string(s), pos);
}
// (4)
template <class charT, class traits, class Allocator>
size_type basic_string<charT, traits, Allocator>::find(charT c, size_type pos = 0) const
{
return find(std::basic_string(1, c), pos);
}
関連項目
| 名前 | 説明 |
|---|---|
std::search |
指定された最初のサブシーケンスを検索する |
std::find |
指定された値を検索する |
contains |
指定された文字・文字列が含まれているかを判定する |
参照
- LWG2064 - More
noexceptissues inbasic_string - P0254R2 Integrating
std::string_viewandstd::string - P0758R1 Implicit conversion traits and utility functions
- LWG Issue 2946. LWG 2758's resolution missed further corrections
- 意図しない暗黙変換防止のために
string_viewを受けるオーバーロード(5)の引数型をconst T&に変更
- 意図しない暗黙変換防止のために
- P0980R1 Making
std::stringconstexpr