Namespaces
Variants

std::ranges:: find_last, std::ranges:: find_last_if, std::ranges:: find_last_if_not

From cppreference.net
< cpp โ€Ž | algorithm โ€Ž | ranges
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
ํ—ค๋” ํŒŒ์ผ์— ์ •์˜๋จ <algorithm>
ํ˜ธ์ถœ ์‹œ๊ทธ๋‹ˆ์ฒ˜
(1)
template < std:: forward_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 ranges:: subrange < I >

find_last ( I first, S last, const T & value, Proj proj = { } ) ;
(C++23๋ถ€ํ„ฐ)
(C++26๊นŒ์ง€)
template < std:: forward_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 ranges:: subrange < I >

find_last ( I first, S last, const T & value, Proj proj = { } ) ;
(C++26๋ถ€ํ„ฐ)
(2)
template < ranges:: forward_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_subrange_t < R >

find_last ( R && r, const T & value, Proj proj = { } ) ;
(C++23๋ถ€ํ„ฐ)
(C++26 ์ด์ „๊นŒ์ง€)
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 >

find_last ( R && r, const T & value, Proj proj = { } ) ;
(C++26๋ถ€ํ„ฐ)
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 >

find_last_if ( I first, S last, Pred pred, Proj proj = { } ) ;
(3) (C++23๋ถ€ํ„ฐ)
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 >

find_last_if ( R && r, Pred pred, Proj proj = { } ) ;
(4) (C++23 ์ดํ›„)
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 >

find_last_if_not ( I first, S last, Pred pred, Proj proj = { } ) ;
(5) (C++23 ์ดํ›„)
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 >

find_last_if_not ( R && r, Pred pred, Proj proj = { } ) ;
(6) (C++23 ์ดํ›„)

๋ฒ”์œ„ [ first , last ) ๋‚ด์—์„œ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

1) find_last ๋Š” value ์™€ ๋™์ผํ•œ ์š”์†Œ๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.
3) find_last_if ๋Š” ๋ฒ”์œ„ [ first , last ) ์—์„œ ์ˆ ์–ด pred ๊ฐ€ true ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.
5) find_last_if_not ๋Š” ๋ฒ”์œ„ [ first , last ) ์—์„œ ์ˆ ์–ด pred ๊ฐ€ false ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.
2,4,6) (1,3,5) ์™€ ๋™์ผํ•˜์ง€๋งŒ, r ๋ฅผ ์†Œ์Šค ๋ฒ”์œ„๋กœ ์‚ฌ์šฉํ•˜๋ฉฐ, ๋งˆ์น˜ ranges:: begin ( r ) ๋ฅผ first ๋กœ, ranges:: end ( r ) ๋ฅผ last ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€์—์„œ ์„ค๋ช…ํ•˜๋Š” ํ•จ์ˆ˜ํ˜• ๊ฐœ์ฒด๋“ค์€ algorithm function objects (์ผ๋ฐ˜์ ์œผ๋กœ niebloids ๋กœ ์•Œ๋ ค์ง„)์ž…๋‹ˆ๋‹ค. ์ฆ‰:

๋ชฉ์ฐจ

๋งค๊ฐœ๋ณ€์ˆ˜

first, last - ๊ฒ€์‚ฌํ•  ์š”์†Œ๋“ค์˜ ๋ฒ”์œ„ ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐ˜๋ณต์ž-๊ฐ์‹œ์ž ์Œ
r - ๊ฒ€์‚ฌํ•  ์š”์†Œ๋“ค์˜ ๋ฒ”์œ„
value - ์š”์†Œ๋“ค๊ณผ ๋น„๊ตํ•  ๊ฐ’
pred - ํˆฌ์˜๋œ ์š”์†Œ๋“ค์— ์ ์šฉํ•  ์กฐ๊ฑด์ž
proj - ์š”์†Œ๋“ค์— ์ ์šฉํ•  ํˆฌ์˜

๋ฐ˜ํ™˜๊ฐ’

1,3,5) i ๊ฐ€ ๋ฒ”์œ„ [ first , last ) ์—์„œ E ๊ฐ€ true ์ธ ๋งˆ์ง€๋ง‰ ๋ฐ˜๋ณต์ž๋ผ๊ณ  ํ•˜์ž.
๋ฐ˜ํ™˜ ranges:: subrange < I > { i, last } , ๋˜๋Š” ranges:: subrange < I > { last, last } ํ•ด๋‹น ๋ฐ˜๋ณต์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ.
1) E ๋Š” bool ( std:: invoke ( proj, * i ) == value ) ์ž…๋‹ˆ๋‹ค.
3) E ๋Š” bool ( std:: invoke ( pred, std:: invoke ( proj, * i ) ) ) ์ž…๋‹ˆ๋‹ค.
5) E ๋Š” bool ( ! std:: invoke ( pred, std:: invoke ( proj, * i ) ) ) ์ž…๋‹ˆ๋‹ค.
2,4,6) (1,3,5) ์™€ ๋™์ผํ•˜์ง€๋งŒ ๋ฐ˜ํ™˜ ํƒ€์ž…์€ ranges:: borrowed_subrange_t < 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) ๋ชฉ๋ก ์ดˆ๊ธฐํ™” for algorithms ( 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
    {
        // ์ฐธ๊ณ : I๊ฐ€ ๋‹จ์ˆœ forward_iterator์ธ ๊ฒฝ์šฐ, begin๋ถ€ํ„ฐ 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
    {
        // ์ฐธ๊ณ : I๊ฐ€ ๋‹จ์ˆœ forward_iterator์ธ ๊ฒฝ์šฐ, begin๋ถ€ํ„ฐ 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
    {
        // ์ฐธ๊ณ : I๊ฐ€ ๋‹จ์ˆœ forward_iterator์ธ ๊ฒฝ์šฐ, begin๋ถ€ํ„ฐ 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
    {
        {"ํ•˜๋‚˜", 1}, {"two", 2}, {"three", 3},
        {"ํ•˜๋‚˜", 4}, {"two", 5}, {"three", 6},
    };
    auto cmp_one = [](const std::string_view &s) { return s == "ํ•˜๋‚˜"; };
    // comparator๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๊ฐ€์žฅ ์ตœ์‹  ์š”์†Œ๋ฅผ ์ฐพ๊ณ , pair::first๋ฅผ ํˆฌ์˜ํ•ฉ๋‹ˆ๋‹ค
    const auto subrange = ranges::find_last_if(list, cmp_one, &P::first);
    std::cout << "๋ฐœ๊ฒฌ๋œ ์š”์†Œ์™€ ๊ทธ ๋’ค์˜ ๊ผฌ๋ฆฌ ๋ถ€๋ถ„์€:\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);
}

์ถœ๋ ฅ:

๋ฐœ๊ฒฌ๋œ ์š”์†Œ์™€ ๊ทธ ๋’ค์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์€:
{"one", 4} {"two", 5} {"three", 6}

์ฐธ๊ณ  ํ•ญ๋ชฉ

ํŠน์ • ๋ฒ”์œ„์—์„œ ๋งˆ์ง€๋ง‰ ์›์†Œ ์‹œํ€€์Šค๋ฅผ ์ฐพ์Œ
(์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•จ์ˆ˜ ๊ฐ์ฒด)
ํŠน์ • ๊ธฐ์ค€์„ ๋งŒ์กฑํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ฅผ ์ฐพ์Œ
(์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•จ์ˆ˜ ๊ฐ์ฒด)
์›์†Œ ๋ฒ”์œ„์˜ ์ฒซ ๋ฒˆ์งธ ๋ฐœ์ƒ์„ ๊ฒ€์ƒ‰ํ•จ
(์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•จ์ˆ˜ ๊ฐ์ฒด)
ํ•œ ์‹œํ€€์Šค๊ฐ€ ๋‹ค๋ฅธ ์‹œํ€€์Šค์˜ ๋ถ€๋ถ„ ์‹œํ€€์Šค์ด๋ฉด true ๋ฅผ ๋ฐ˜ํ™˜ํ•จ
(์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•จ์ˆ˜ ๊ฐ์ฒด)
๋ถ€๋ถ„์ ์œผ๋กœ ์ •๋ ฌ๋œ ๋ฒ”์œ„์— ์›์†Œ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•จ
(์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•จ์ˆ˜ ๊ฐ์ฒด)
๋ฒ”์œ„๊ฐ€ ์ฃผ์–ด์ง„ ์›์†Œ๋‚˜ ๋ถ€๋ถ„ ๋ฒ”์œ„๋ฅผ ํฌํ•จํ•˜๋Š”์ง€ ํ™•์ธํ•จ
(์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•จ์ˆ˜ ๊ฐ์ฒด)