Namespaces
Variants

std::ranges:: find, std::ranges:: find_if, std::ranges:: find_if_not

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
(注:指定されたHTML要素には翻訳対象のテキストコンテンツが含まれていないため、翻訳作業は不要です。HTML構造と属性はそのまま保持されています。)
定義済みヘッダー <algorithm>
呼び出しシグネチャ
(1)
template < std:: input_iterator I, std:: sentinel_for < I > S,

class T, class Proj = std:: identity >
requires std:: indirect_binary_predicate
< ranges:: equal_to , std :: projected < I, Proj > , const T * >

constexpr I find ( I first, S last, const T & value, Proj proj = { } ) ;
(C++20以降)
(C++26まで)
template < std:: input_iterator I, std:: sentinel_for < I > S,

class Proj = std:: identity ,
class T = std :: projected_value_t < I, Proj > >
requires std:: indirect_binary_predicate
< ranges:: equal_to , std :: projected < I, Proj > , const T * >

constexpr I find ( I first, S last, const T & value, Proj proj = { } ) ;
(C++26以降)
(2)
template < ranges:: input_range R, class T, class Proj = std:: identity >

requires std:: indirect_binary_predicate
< ranges:: equal_to ,
std :: projected < ranges:: iterator_t < R > , Proj > , const T * >
constexpr ranges:: borrowed_iterator_t < R >

find ( R && r, const T & value, Proj proj = { } ) ;
(C++20以降)
(C++26まで)
template < ranges:: input_range R, class Proj = std:: identity ,

class T = std :: projected_value_t < ranges:: iterator_t < R > , Proj > >
requires std:: indirect_binary_predicate
< ranges:: equal_to ,
std :: projected < ranges:: iterator_t < R > , Proj > , const T * >
constexpr ranges:: borrowed_iterator_t < R >

find ( R && r, const T & value, Proj proj = { } ) ;
(C++26以降)
template < std:: input_iterator I, std:: sentinel_for < I > S,

class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < I, Proj >> Pred >

constexpr I find_if ( I first, S last, Pred pred, Proj proj = { } ) ;
(3) (C++20以降)
template < ranges:: input_range R, class Proj = std:: identity ,

std:: indirect_unary_predicate
< std :: projected < ranges:: iterator_t < R > , Proj >> Pred >
constexpr ranges:: borrowed_iterator_t < R >

find_if ( R && r, Pred pred, Proj proj = { } ) ;
(4) (C++20以降)
template < std:: input_iterator I, std:: sentinel_for < I > S,

class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < I, Proj >> Pred >

constexpr I find_if_not ( I first, S last, Pred pred, Proj proj = { } ) ;
(5) (C++20以降)
template < ranges:: input_range R, class Proj = std:: identity ,

std:: indirect_unary_predicate
< std :: projected < ranges:: iterator_t < R > , Proj >> Pred >
constexpr ranges:: borrowed_iterator_t < R >

find_if_not ( R && r, Pred pred, Proj proj = { } ) ;
(6) (C++20以降)

範囲 [ first , last ) 内で特定の条件を満たす最初の要素を返します:

1) find value に等しい要素を検索します。
3) find_if は述語 pred true を返す要素を検索します。
5) find_if_not は述語 pred false を返す要素を検索します。
2,4,6) (1,3,5) と同じですが、 r をソース範囲として使用します。これは ranges:: begin ( r ) first として、 ranges:: end ( r ) last として使用する場合と同様です。

このページで説明されている関数ライクな実体は、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる)です。つまり:

目次

パラメータ

first, last - 検査対象の要素の 範囲 を定義するイテレータ-センチネルペア
r - 検査対象の要素の範囲
value - 要素と比較する値
pred - 投影された要素に適用する述語
proj - 要素に適用する投影

戻り値

条件を満たす最初の要素へのイテレータ、またはそのような要素が見つからない場合は last と等しいイテレータ。

計算量

最大で last - first 回の述語と射影の適用。

実装例

**日本語訳:** **説明:** - HTMLタグと属性はそのまま保持されています - `find (1)` はC++の関数名/コマンド名として扱われ、翻訳対象外としました - 数値の`(1)`はバージョン番号を示すものと判断し、翻訳しませんでした **注記**: 提供されたコードはC++のテンプレートコードであり、HTMLタグ内の`
`ブロックに含まれています。指示に従い、HTMLタグ、属性、および`
`タグ内のC++コードは翻訳せず、そのまま保持しています。C++のキーワード、型名、関数名などの専門用語は翻訳対象外です。
このC++コードは`find_if`関数オブジェクトの実装を表しています。HTMLタグ、属性、および`
`タグ内のC++コードはすべて原文のまま保持されています。
**日本語訳:** **説明:** - `find_if_not` はC++の標準ライブラリ関数名であるため、翻訳対象外としました - HTMLタグと属性は完全に保持されています - 数値の(5)はバージョン番号を示すものと判断し、そのまま保持しました **注記**: このC++コードは技術的な内容であり、HTMLタグ、属性、および` `、`
`タグ内のテキストは翻訳対象外と指定されているため、元のコードは完全に保持されています。C++固有の用語(`struct`、`template`、`constexpr`、`operator()`など)も翻訳せず、元のまま維持されています。
find (1)
find (1)
struct find_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S,
             class Proj = std::identity,
             class T = std::projected_value_t<I, Proj>>
    requires std::indirect_binary_predicate
                 <ranges::equal_to, std::projected<I, Proj>, const T*>
    constexpr I operator()(I first, S last, const T& value, Proj proj = {}) const
    {
        for (; first != last; ++first)
            if (std::invoke(proj, *first) == value)
                return first;
        return first;
    }
    template<ranges::input_range R, class T, class Proj = std::identity>
    requires std::indirect_binary_predicate<ranges::equal_to,
                 std::projected<ranges::iterator_t<R>, Proj>, const T*>
    constexpr ranges::borrowed_iterator_t<R>
        operator()(R&& r, const T& value, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), value, std::ref(proj));
    }
};
inline constexpr find_fn find;
find_if (3)
struct find_if_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    constexpr I operator()(I first, S last, Pred pred, Proj proj = {}) const
    {
        for (; first != last; ++first)
            if (std::invoke(pred, std::invoke(proj, *first)))
                return first;
        return first;
    }
    template<ranges::input_range R, class Proj = std::identity,
             std::indirect_unary_predicate
                 <std::projected<ranges::iterator_t<R>, Proj>> Pred>
    constexpr ranges::borrowed_iterator_t<R>
        operator()(R&& r, Pred pred, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
    }
};
inline constexpr find_if_fn find_if;
find_if_not (5)
find_if_not (5)
struct find_if_not_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    constexpr I operator()(I first, S last, Pred pred, Proj proj = {}) const
    {
        for (; first != last; ++first)
            if (!std::invoke(pred, std::invoke(proj, *first)))
                return first;
        return first;
    }
    template<ranges::input_range R, class Proj = std::identity,
             std::indirect_unary_predicate
                 <std::projected<ranges::iterator_t<R>, Proj>> Pred>
    constexpr ranges::borrowed_iterator_t<R>
        operator()(R&& r, Pred pred, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
    }
};
inline constexpr find_if_not_fn find_if_not;

注記

機能テスト マクロ 標準 機能
__cpp_lib_algorithm_default_value_type 202403 (C++26) リスト初期化 アルゴリズム用 ( 1,2 )

#include <algorithm>
#include <cassert>
#include <complex>
#include <format>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
void projector_example()
{
    struct folk_info
    {
        unsigned uid;
        std::string name, position;
    };
    std::vector<folk_info> folks
    {
        {0, "アナ", "dev"},
        {1, "ボブ", "devops"},
        {2, "イブ", "ops"}
    };
    const auto who{"イブ"};
    if (auto it = std::ranges::find(folks, who, &folk_info::name); it != folks.end())
        std::cout << std::format("プロフィール:\n"
                                 "    UID: {}\n"
                                 "    名前: {}\n"
                                 "    位置: {}\n\n",
                                 it->uid, it->name, it->position);
}
int main()
{
    namespace ranges = std::ranges;
    projector_example();
    const int n1 = 3;
    const int n2 = 5;
    const auto v = {4, 1, 3, 2};
    if (ranges::find(v, n1) != v.end())
        std::cout << "v contains: " << n1 << '\n';
    else
        std::cout << "v は次を含みません: " << n1 << '\n';
    if (ranges::find(v.begin(), v.end(), n2) != v.end())
        std::cout << "v contains: " << n2 << '\n';
    else
        std::cout << "v は次を含みません: " << n2 << '\n';
    auto is_even = [](int x) { return x % 2 == 0; };
    if (auto result = ranges::find_if(v.begin(), v.end(), is_even); result != v.end())
        std::cout << "v内の最初の偶数要素: " << *result << '\n';
    else
        std::cout << "vに偶数の要素はありません\n";
    if (auto result = ranges::find_if_not(v, is_even); result != v.end())
        std::cout << "v内の最初の奇数要素: " << *result << '\n';
    else
        std::cout << "vに奇数要素はありません\n";
    auto divides_13 = []
(注:元のテキストが単一の閉じ角括弧のみであるため、HTMLタグを保持したまま、内容を日本語に翻訳する必要はありません。指定された条件に従い、この記号はそのまま維持されます。)(int x) { return x % 13 == 0; };
    if (auto result = ranges::find_if(v, divides_13); result != v.end())
        std::cout << "v内で13で割り切れる最初の要素: " << *result << '\n';
    else
        std::cout << "v内の要素に13で割り切れるものはありません\n";
    if (auto result = ranges::find_if_not(v.begin(), v.end(), divides_13);
        result != v.end())
        std::cout << "v内の13で割り切れない最初の要素: " << *result << '\n';
    else
        std::cout << "v内のすべての要素は13で割り切れます\n";
    std::vector<std::complex<double>> nums{{4, 2}};
    #ifdef __cpp_lib_algorithm_default_value_type
        // Tは(2)で推論され、リスト初期化が可能になる
        const auto it = ranges::find(nums, {4, 2});
    #else
        const auto it = ranges::find(nums, std::complex<double>{4, 2});
    #endif
    assert(it == nums.begin());
}

出力:

プロフィール:
    UID: 2
    名前: Eve
    役職: ops
v に含まれる要素数: 3
v に含まれない要素: 5
v 内の最初の偶数要素: 4
v 内の最初の奇数要素: 1
v 内の要素は13で割り切れるものはありません
v 内の13で割り切れない最初の要素: 4

関連項目

等しい(または与えられた述語を満たす)最初の隣接する2つの要素を検索する
(アルゴリズム関数オブジェクト)
特定の範囲内で要素シーケンスの最後の出現を検索する
(アルゴリズム関数オブジェクト)
要素セットのいずれかを検索する
(アルゴリズム関数オブジェクト)
2つの範囲が最初に異なる位置を検索する
(アルゴリズム関数オブジェクト)
要素の範囲の最初の出現を検索する
(アルゴリズム関数オブジェクト)
特定の基準を満たす最初の要素を検索する
(関数テンプレート)