跳到主要内容

std::ranges::find_last_if_not() 算法

// (1)
constexpr ranges::subrange<I>
find_last_if_not( I first, S last, Pred pred, Proj proj = {} );

// (2)
constexpr ranges::borrowed_subrange_t<R>
find_last_if_not( R&& r, Pred pred, Proj proj = {} );

参数类型是泛型的,并具有以下约束

  • I - std::forward_iterator
  • S - std::sentinel_for<I>
  • Proj - (无)
  • Pred:
    • (1) - std::indirect_unary_predicate<std::projected<I, Proj>>
    • (2) - std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>>
  • (2) - R - std::ranges::input_range

对于所有重载,Proj 模板参数的默认类型为 std::identity

返回范围 [first; last) 中满足特定条件的最后一个元素

  • (1) 搜索谓词 pred 返回 false 的元素。

  • (2)(1) 相同,但使用 r 作为源范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

本页描述的函数类实体是niebloids

参数

first
last

要检查的元素范围。

r

要检查的元素范围。

pred

应用于投影元素的谓词。

proj

应用于元素的投影。

返回值

  • (1)i 为范围 [first; last) 中最后一个满足 *i == valuetrue 的迭代器。返回一个类型为 ranges::subrange<I> 的值,初始化如下

    {
    i,
    last
    }

    如果没有找到这样的迭代器,则返回 { last, last }

  • (2)(1) 相同,但返回类型为 ranges::borrowed_subrange_t<I>

复杂度

谓词和投影最多应用 last - first 次。

异常

(无)

可能的实现

find_last_if_not(1) 和 find_last_if_not(2)
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.
I found {};
for (; first != last; ++first)
if (!std::invoke(pred, std::invoke(proj, *first)))
found = first;

if (found == I {})
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;

示例

Main.cpp
#include <algorithm>
#include <forward_list>
#include <iomanip>
#include <iostream>
#include <string_view>

int main()
{
constexpr static auto v = {1, 2, 3, 1, 2, 3, 1, 2};

{
constexpr auto i1 = std::ranges::find_last(v.begin(), v.end(), 3);
constexpr auto i2 = std::ranges::find_last(v, 3);
static_assert(std::ranges::distance(v.begin(), i1.begin()) == 5);
static_assert(std::ranges::distance(v.begin(), i2.begin()) == 5);
}
{
constexpr auto i1 = std::ranges::find_last(v.begin(), v.end(), -3);
constexpr auto i2 = std::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 = std::ranges::find_last_if_not(v.begin(), v.end(), pred, abs);
constexpr auto i2 = std::ranges::find_last_if_not(v, pred, abs);
static_assert(std::ranges::distance(v.begin(), i1.begin()) == 5);
static_assert(std::ranges::distance(v.begin(), i2.begin()) == 5);
}
{
auto pred = [](int x) { return x == -3; };
constexpr auto i1 = std::ranges::find_last_if_not(v.begin(), v.end(), pred, abs);
constexpr auto i2 = std::ranges::find_last_if_not(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 = std::ranges::find_last_if_not_not(v.begin(), v.end(), pred, abs);
constexpr auto i2 = std::ranges::find_last_if_not_not(v, pred, abs);
static_assert(std::ranges::distance(v.begin(), i1.begin()) == 5);
static_assert(std::ranges::distance(v.begin(), i2.begin()) == 5);
}
{
auto pred = [](int x) { return x == 1 or x == 2 or x == 3; };
constexpr auto i1 = std::ranges::find_last_if_not_not(v.begin(), v.end(), pred, abs);
constexpr auto i2 = std::ranges::find_last_if_not_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 = std::ranges::find_last_if_not(list, cmp_one, &P::first);

// print the found element and the "tail" after it
for (P const& e : subrange)
std::cout << '{' << std::quoted(e.first) << ", " << e.second << "} ";
std::cout << '\n';
}
输出
{"one", 4} {"two", 5} {"three", 6}
本文档来源于此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::ranges::find_last_if_not() 算法

// (1)
constexpr ranges::subrange<I>
find_last_if_not( I first, S last, Pred pred, Proj proj = {} );

// (2)
constexpr ranges::borrowed_subrange_t<R>
find_last_if_not( R&& r, Pred pred, Proj proj = {} );

参数类型是泛型的,并具有以下约束

  • I - std::forward_iterator
  • S - std::sentinel_for<I>
  • Proj - (无)
  • Pred:
    • (1) - std::indirect_unary_predicate<std::projected<I, Proj>>
    • (2) - std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>>
  • (2) - R - std::ranges::input_range

对于所有重载,Proj 模板参数的默认类型为 std::identity

返回范围 [first; last) 中满足特定条件的最后一个元素

  • (1) 搜索谓词 pred 返回 false 的元素。

  • (2)(1) 相同,但使用 r 作为源范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

本页描述的函数类实体是niebloids

参数

first
last

要检查的元素范围。

r

要检查的元素范围。

pred

应用于投影元素的谓词。

proj

应用于元素的投影。

返回值

  • (1)i 为范围 [first; last) 中最后一个满足 *i == valuetrue 的迭代器。返回一个类型为 ranges::subrange<I> 的值,初始化如下

    {
    i,
    last
    }

    如果没有找到这样的迭代器,则返回 { last, last }

  • (2)(1) 相同,但返回类型为 ranges::borrowed_subrange_t<I>

复杂度

谓词和投影最多应用 last - first 次。

异常

(无)

可能的实现

find_last_if_not(1) 和 find_last_if_not(2)
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.
I found {};
for (; first != last; ++first)
if (!std::invoke(pred, std::invoke(proj, *first)))
found = first;

if (found == I {})
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;

示例

Main.cpp
#include <algorithm>
#include <forward_list>
#include <iomanip>
#include <iostream>
#include <string_view>

int main()
{
constexpr static auto v = {1, 2, 3, 1, 2, 3, 1, 2};

{
constexpr auto i1 = std::ranges::find_last(v.begin(), v.end(), 3);
constexpr auto i2 = std::ranges::find_last(v, 3);
static_assert(std::ranges::distance(v.begin(), i1.begin()) == 5);
static_assert(std::ranges::distance(v.begin(), i2.begin()) == 5);
}
{
constexpr auto i1 = std::ranges::find_last(v.begin(), v.end(), -3);
constexpr auto i2 = std::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 = std::ranges::find_last_if_not(v.begin(), v.end(), pred, abs);
constexpr auto i2 = std::ranges::find_last_if_not(v, pred, abs);
static_assert(std::ranges::distance(v.begin(), i1.begin()) == 5);
static_assert(std::ranges::distance(v.begin(), i2.begin()) == 5);
}
{
auto pred = [](int x) { return x == -3; };
constexpr auto i1 = std::ranges::find_last_if_not(v.begin(), v.end(), pred, abs);
constexpr auto i2 = std::ranges::find_last_if_not(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 = std::ranges::find_last_if_not_not(v.begin(), v.end(), pred, abs);
constexpr auto i2 = std::ranges::find_last_if_not_not(v, pred, abs);
static_assert(std::ranges::distance(v.begin(), i1.begin()) == 5);
static_assert(std::ranges::distance(v.begin(), i2.begin()) == 5);
}
{
auto pred = [](int x) { return x == 1 or x == 2 or x == 3; };
constexpr auto i1 = std::ranges::find_last_if_not_not(v.begin(), v.end(), pred, abs);
constexpr auto i2 = std::ranges::find_last_if_not_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 = std::ranges::find_last_if_not(list, cmp_one, &P::first);

// print the found element and the "tail" after it
for (P const& e : subrange)
std::cout << '{' << std::quoted(e.first) << ", " << e.second << "} ";
std::cout << '\n';
}
输出
{"one", 4} {"two", 5} {"three", 6}
本文档来源于此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。