std::ranges::find_last, std::ranges::find_last_if, std::ranges::find_last_if_not
| ヘッダー <algorithm> で定義 |
||
| 呼び出しシグネチャ |
||
| (1) | ||
template< std::forward_iterator I, std::sentinel_for<I> S, class T, |
(C++23から) (C++26まで) |
|
| template< std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity, |
(C++26以降) | |
| (2) | ||
template< ranges::forward_range R, class T, |
(C++23から) (C++26まで) |
|
| template< ranges::forward_range R, class Proj = std::identity, |
(C++26以降) | |
template< std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity, |
(3) | (C++23から) |
| template< ranges::forward_range R, class Proj = std::identity, |
(4) | (C++23から) |
template< std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity, |
(5) | (C++23から) |
| template< ranges::forward_range R, class Proj = std::identity, |
(6) | (C++23から) |
特定条件を満たす、範囲 [first, last) 内の最後の要素を返します。
find_last は value と等しい要素を検索します。find_last_if は、述語 pred が true を返す、範囲 [first, last) 内の最後の要素を検索します。find_last_if_not は、述語 pred が false を返す、範囲 [first, last) 内の最後の要素を検索します。このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式にはニーブロイドとして知られている)です。つまり、
- これらのいずれかを呼び出す際に、明示的なテンプレート引数リストを指定することはできません。
- これらのいずれも実引数依存の名前探索には見えません。
- これらのいずれかが関数呼び出し演算子の左側の名前として通常の非修飾名探索によって見つかった場合、実引数依存の名前探索は抑制されます。
目次 |
[編集] パラメータ
| first, last | - | 調査する要素の範囲を定義するイテレータとセンチネルのペア |
| r | - | 検査する要素の範囲 |
| value | - | 要素と比較する値 |
| pred | - | 射影された要素に適用する述語 |
| proj | - | 要素に適用する射影 |
[編集] 戻り値
[first, last) 内の最後のイテレータを i とします。[編集] 計算量
述語と射影の適用回数は最大で last - first 回。
[編集] 備考
ranges::find_last、ranges::find_last_if、ranges::find_last_if_not は、I が bidirectional_iterator または (より良い) random_access_iterator のモデルである場合、一般的な実装においてより効率的です。
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_ranges_find_last |
202207L |
(C++23) | ranges::find_last,ranges::find_last_if,ranges::find_last_if_not
|
__cpp_lib_algorithm_default_value_type |
202403L |
(C++26) | アルゴリズム (1,2) のためのリスト初期化 |
[編集] 可能な実装
これらの実装は、I が forward_iterator のモデルである場合に使用される遅いアルゴリズムのみを示しています。
| find_last (1,2) |
|---|
struct find_last_fn { template<std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity, class T = std::projected_value_t<iterator_t<R>, Proj>> requires std::indirect_binary_predicate <ranges::equal_to, std::projected<I, Proj>, const T*> constexpr ranges::subrange<I> operator()(I first, S last, const T &value, Proj proj = {}) const { // Note: if I is mere forward_iterator, we may only go from begin to end. std::optional<I> found; for (; first != last; ++first) if (std::invoke(proj, *first) == value) found = first; if (!found) return {first, first}; return {*found, std::ranges::next(*found, last)}; } template<ranges::forward_range R, class Proj = std::identity, class T = std::projected_value_t<iterator_t<R>, Proj>> requires std::indirect_binary_predicate <ranges::equal_to, std::projected<ranges::iterator_t<R>, Proj>, const T*> constexpr ranges::borrowed_subrange_t<R> operator()(R&& r, const T &value, Proj proj = {}) const { return this->operator()(ranges::begin(r), ranges::end(r), value, std::ref(proj)); } }; inline constexpr find_last_fn find_last; |
| find_last_if (3,4) |
struct find_last_if_fn { template<std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred> constexpr ranges::subrange<I> operator()(I first, S last, Pred pred, Proj proj = {}) const { // Note: if I is mere forward_iterator, we may only go from begin to end. std::optional<I> found; for (; first != last; ++first) if (std::invoke(pred, std::invoke(proj, *first))) found = first; if (!found) return {first, first}; return {*found, std::ranges::next(*found, last)}; } template<ranges::forward_range R, class Proj = std::identity, std::indirect_unary_predicate <std::projected<ranges::iterator_t<R>, Proj>> Pred> constexpr ranges::borrowed_subrange_t<R> operator()(R&& r, Pred pred, Proj proj = {}) const { return this->operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } }; inline constexpr find_last_if_fn find_last_if; |
| find_last_if_not (5,6) |
struct find_last_if_not_fn { template<std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred> constexpr ranges::subrange<I> operator()(I first, S last, Pred pred, Proj proj = {}) const { // Note: if I is mere forward_iterator, we may only go from begin to end. std::optional<I> found; for (; first != last; ++first) if (!std::invoke(pred, std::invoke(proj, *first))) found = first; if (!found) return {first, first}; return {*found, std::ranges::next(*found, last)}; } template<ranges::forward_range R, class Proj = std::identity, std::indirect_unary_predicate <std::projected<ranges::iterator_t<R>, Proj>> Pred> constexpr ranges::borrowed_subrange_t<R> operator()(R&& r, Pred pred, Proj proj = {}) const { return this->operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } }; inline constexpr find_last_if_not_fn find_last_if_not; |
[編集] 例
#include <algorithm> #include <cassert> #include <forward_list> #include <iomanip> #include <iostream> #include <string_view> int main() { namespace ranges = std::ranges; constexpr static auto v = {1, 2, 3, 1, 2, 3, 1, 2}; { constexpr auto i1 = ranges::find_last(v.begin(), v.end(), 3); constexpr auto i2 = ranges::find_last(v, 3); static_assert(ranges::distance(v.begin(), i1.begin()) == 5); static_assert(ranges::distance(v.begin(), i2.begin()) == 5); } { constexpr auto i1 = ranges::find_last(v.begin(), v.end(), -3); constexpr auto i2 = ranges::find_last(v, -3); static_assert(i1.begin() == v.end()); static_assert(i2.begin() == v.end()); } auto abs = [](int x) { return x < 0 ? -x : x; }; { auto pred = [](int x) { return x == 3; }; constexpr auto i1 = ranges::find_last_if(v.begin(), v.end(), pred, abs); constexpr auto i2 = ranges::find_last_if(v, pred, abs); static_assert(ranges::distance(v.begin(), i1.begin()) == 5); static_assert(ranges::distance(v.begin(), i2.begin()) == 5); } { auto pred = [](int x) { return x == -3; }; constexpr auto i1 = ranges::find_last_if(v.begin(), v.end(), pred, abs); constexpr auto i2 = ranges::find_last_if(v, pred, abs); static_assert(i1.begin() == v.end()); static_assert(i2.begin() == v.end()); } { auto pred = [](int x) { return x == 1 or x == 2; }; constexpr auto i1 = ranges::find_last_if_not(v.begin(), v.end(), pred, abs); constexpr auto i2 = ranges::find_last_if_not(v, pred, abs); static_assert(ranges::distance(v.begin(), i1.begin()) == 5); static_assert(ranges::distance(v.begin(), i2.begin()) == 5); } { auto pred = [](int x) { return x == 1 or x == 2 or x == 3; }; constexpr auto i1 = ranges::find_last_if_not(v.begin(), v.end(), pred, abs); constexpr auto i2 = ranges::find_last_if_not(v, pred, abs); static_assert(i1.begin() == v.end()); static_assert(i2.begin() == v.end()); } using P = std::pair<std::string_view, int>; std::forward_list<P> list { {"one", 1}, {"two", 2}, {"three", 3}, {"one", 4}, {"two", 5}, {"three", 6}, }; auto cmp_one = [](const std::string_view &s) { return s == "one"; }; // find latest element that satisfy the comparator, and projecting pair::first const auto subrange = ranges::find_last_if(list, cmp_one, &P::first); std::cout << "The found element and the tail after it are:\n"; for (P const& e : subrange) std::cout << '{' << std::quoted(e.first) << ", " << e.second << "} "; std::cout << '\n'; #if __cpp_lib_algorithm_default_value_type const auto i3 = ranges::find_last(list, {"three", 3}); // (2) C++26 #else const auto i3 = ranges::find_last(list, P{"three", 3}); // (2) C++23 #endif assert(i3.begin()->first == "three" && i3.begin()->second == 3); }
出力
The found element and the tail after it are:
{"one", 4} {"two", 5} {"three", 6}[編集] 関連項目
| (C++20) |
特定の範囲内で最後の要素のシーケンスを見つける (アルゴリズム関数オブジェクト) |
| (C++20)(C++20)(C++20) |
特定の基準を満たす最初の要素を見つける (アルゴリズム関数オブジェクト) |
| (C++20) |
要素の範囲の最初の出現を検索する (アルゴリズム関数オブジェクト) |
| (C++20) |
あるシーケンスが別のシーケンスの部分シーケンスである場合に true を返す (アルゴリズム関数オブジェクト) |
| (C++20) |
部分的に順序付けられた範囲に要素が存在するかどうかを判断する (アルゴリズム関数オブジェクト) |
| (C++23)(C++23) |
範囲が指定された要素または部分範囲を含むかチェックする (アルゴリズム関数オブジェクト) |