std::ranges::find_end
ja.cppreference.net より
| ヘッダで定義 <algorithm>
|
||
| 呼び出しシグネチャ |
||
template< std::forward_iterator I1, std::sentinel_for<I1> S1,
std::forward_iterator I2, std::sentinel_for<I2> S2,
class Pred = ranges::equal_to,
class Proj1 = std::identity, class Proj2 = std::identity >
requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
constexpr ranges::subrange<I1>
find_end( I1 first1, S1 last1, I2 first2, S2 last2,
Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
|
(1) | (C++20以降) |
template< ranges::forward_range R1, ranges::forward_range R2,
class Pred = ranges::equal_to,
class Proj1 = std::identity, class Proj2 = std::identity >
requires std::indirectly_comparable<ranges::iterator_t<R1>,
ranges::iterator_t<R2>,
Pred, Proj1, Proj2>
constexpr ranges::borrowed_subrange_t<R1>
find_end( R1&& r1, R2&& r2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {} );
|
(2) | (C++20以降) |
template< /*execution-policy*/ Ep,
std::random_access_iterator I1, std::sized_sentinel_for<I1> S1,
std::random_access_iterator I2, std::sized_sentinel_for<I2> S2,
class Pred = ranges::equal_to,
class Proj1 = identity, class Proj2 = identity>
requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
ranges::subrange<I1> find_end( Ep&& policy, I1 first1, S1 last1,
I2 first2, S2 last2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {} );
|
(3) | (C++26以降) |
template< /*execution-policy*/ Ep,
/*sized-random-access-range*/ R1,
/*sized-random-access-range*/ R2,
class Pred = ranges::equal_to,
class Proj1 = identity, class Proj2 = identity>
requires std::indirectly_comparable<ranges::iterator_t<R1>,
ranges::iterator_t<R2>,
Pred, Proj1, Proj2>
ranges::borrowed_subrange_t<R1>
find_end( Ep&& policy, R1&& r1, R2&& r2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {} );
|
(4) | (C++26以降) |
の定義については、/*execution-policy*/、このページを参照してください。の定義については、/*sized-random-access-range*/、このページを参照してください。
ソースレンジ内でターゲットレンジの最後の出現を検索します。要素は (それぞれ proj1 および proj2 によって射影されます) 二項述語 pred を使用して比較されます。
1) ソースレンジは
[first1, last1)、ターゲットレンジは [first2, last2) です。2) ソースレンジは
r1、ターゲットレンジは r2 です。3,4) (1,2) と同じですが、(1,2)、
policy に従って実行されます。このページで説明する関数のようなエンティティは、アルゴリズム関数オブジェクト (通称 niebloids) です。すなわち:
- 明示的なテンプレート引数リストを指定して呼び出すことはできません。
- それらは 引数依存の名前検索には表示されません。
- それらのいずれかが 通常の非修飾名前検索 によって関数呼び出し演算子の左側の名前として見つかった場合、引数依存の名前検索は抑制されます。
パラメータ
| first1, last1 | - | ソース範囲 |
| を定義するイテレータ-センチネルペアfirst2, last2 | - | ターゲット範囲 |
| を定義するイテレータ-センチネルペアr1 | - | ソース範囲 |
| r2 | - | ターゲット範囲 |
| pred | - | 射影された要素に適用される述語 |
| proj1 | - | ソース範囲内の要素に適用される射影 |
| proj2 | - | ターゲット範囲内の要素に適用される射影 |
| policy | - | 実行ポリシーを使用する |
戻り値
ソース範囲内のターゲット範囲の最後の出現に対応する部分範囲。
ターゲット範囲が空であるか、ソース範囲内に現れない場合、空の範囲が返されます。
計算量
ここで、N1 を ranges::distance(first1, last1) または ranges::distance(r1)、そして N2 を ranges::distance(first2, last2) または ranges::distance(r2):
1,2) 最大でも N2⋅(N1-N2+1) の
pred および proj の適用回数。3,4) 𝓞(N2⋅(N1-N2+1)) の
pred および proj の適用回数。例外
3,4) 実行処理中:
- 並列化に必要な一時メモリリソースが利用できない場合、std::bad_alloc がスローされます。
- アルゴリズム引数を介してオブジェクトにアクセス中にキャッチされない例外がスローされた場合、動作は実行ポリシーによって決まります(標準ポリシー、std::terminate が呼び出されます)。
注釈
実装は、イテレータ型がbidirectional_iteratorをモデル化する場合、末尾から先頭に向かって検索することで検索の効率を向上させることができる。random_access_iteratorをモデル化すると比較速度が向上する可能性がある。しかし、これらはすべて最悪の場合の理論的な計算量を変えるものではない。
実装例
struct find_end_fn
{
template<std::forward_iterator I1, std::sentinel_for<I1> S1,
std::forward_iterator I2, std::sentinel_for<I2> S2,
class Pred = ranges::equal_to,
class Proj1 = std::identity, class Proj2 = std::identity>
requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
constexpr ranges::subrange<I1>
operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {}) const
{
if (first2 == last2)
{
auto last_it = ranges::next(first1, last1);
return {last_it, last_it};
}
auto result = ranges::search(std::move(first1), last1,
first2, last2, pred, proj1, proj2);
if (result.empty())
return result;
for (;;)
{
auto new_result = ranges::search(std::next(result.begin()), last1,
first2, last2, pred, proj1, proj2);
if (new_result.empty())
return result;
else
result = std::move(new_result);
}
}
template<ranges::forward_range R1, ranges::forward_range R2,
class Pred = ranges::equal_to,
class Proj1 = std::identity,
class Proj2 = std::identity>
requires std::indirectly_comparable<ranges::iterator_t<R1>,
ranges::iterator_t<R2>,
Pred, Proj1, Proj2>
constexpr ranges::borrowed_subrange_t<R1>
operator()(R1&& r1, R2&& r2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {}) const
{
return (*this)(ranges::begin(r1),
ranges::next(ranges::begin(r1), ranges::end(r1)),
ranges::begin(r2),
ranges::next(ranges::begin(r2), ranges::end(r2)),
std::move(pred), std::move(proj1), std::move(proj2));
}
};
inline constexpr find_end_fn find_end{};
|
例
このコードを実行
#include <algorithm>
#include <array>
#include <cctype>
#include <iostream>
#include <ranges>
#include <string_view>
void print(const auto haystack, const auto needle)
{
const auto pos = std::distance(haystack.begin(), needle.begin());
std::cout << "In \"";
for (const auto c : haystack)
std::cout << c;
std::cout << "\" found \"";
for (const auto c : needle)
std::cout << c;
std::cout << "\" at position [" << pos << ".." << pos + needle.size() << ")\n"
<< std::string(4 + pos, ' ') << std::string(needle.size(), '^') << '\n';
}
int main()
{
using namespace std::literals;
using std::ranges::find_end;
constexpr auto secret{"password password word..."sv};
constexpr auto wanted{"password"sv};
constexpr auto found1 = find_end(secret.cbegin(), secret.cend(),
wanted.cbegin(), wanted.cend());
print(secret, found1);
constexpr auto found2 = find_end(secret, "word"sv);
print(secret, found2);
const auto found3 = find_end(secret, "ORD"sv,
[](const char x, const char y)
{ // uses a binary predicate
return std::tolower(x) == std::tolower(y);
});
print(secret, found3);
const auto found4 = find_end(secret, "SWORD"sv, {}, {},
[](char c) { return std::tolower(c); }); // projects the 2nd range
print(secret, found4);
static_assert(find_end(secret, "PASS"sv).empty()); // => not found
}
出力:
In "password password word..." found "password" at position [9..17)
^^^^^^^^
In "password password word..." found "word" at position [18..22)
^^^^
In "password password word..." found "ord" at position [19..22)
^^^
In "password password word..." found "sword" at position [12..17)
^^^^^
関連項目
| 指定された範囲内の要素の最後のシーケンスを検索します (関数テンプレート) | |
(C++23)(C++23)(C++23) |
特定の条件を満たす最後の要素を見つける (アルゴリズム関数オブジェクト) |
(C++20)(C++20)(C++20) |
特定の条件を満たす最初の要素を見つける (アルゴリズム関数オブジェクト) |
(C++20) |
要素の集合のいずれかを検索する (アルゴリズム関数オブジェクト) |
(C++20) |
等しい(または指定された述語を満たす)最初の隣接する2つの項目を見つける (アルゴリズム関数オブジェクト) |
(C++20) |
ある範囲の要素の最初の出現を検索する (アルゴリズム関数オブジェクト) |
(C++20) |
範囲内の要素が連続して複数回出現する最初の位置を検索する (アルゴリズム関数オブジェクト) |