跳到主要内容

std::ranges::any_of() 算法

// (1)
constexpr bool any_of( I first, S last, Pred pred, Proj proj = {} );

// (2)
constexpr bool any_of( R&& r, Pred pred, Proj proj = {} );

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

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

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

检查谓词是否对范围内的任何元素都成立。

  • (1) 检查一元谓词 pred 是否对范围 [first; last) 中的任何元素(在通过投影 proj 投影后)返回 true

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

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

参数

first
last

要检查的元素范围。

r

要检查的元素范围。

proj

应用于元素的投影。

pred

应用于投影元素的谓词。

返回值

如果范围内至少有一个迭代器 i 使得 std::invoke(pred, std::invoke(proj, *i)) != false,则返回 true,否则返回 false

重要

如果范围为空,则返回 true

复杂度

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

异常

(无)

可能的实现

any_of(1) 和 any_of(2)
struct any_of_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 bool operator()(I first, S last, Pred pred, Proj proj = {}) const
{
return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) != last;
}

template<ranges::input_range R, class Proj = std::identity,
std::indirect_unary_predicate<
std::projected<ranges::iterator_t<R>,Proj>> Pred>
constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const
{
return operator()(ranges::begin(r), ranges::end(r),
std::ref(pred), std::ref(proj));
}
};

inline constexpr any_of_fn any_of;

示例

Main.cpp
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>

namespace ranges = std::ranges;

constexpr bool some_of(auto&& r, auto&& pred) // some but not any
{
return not (ranges::any_of(r, pred) or ranges::none_of(r, pred));
}

constexpr auto w = { 1, 2, 3 };
static_assert(!some_of(w, [](int x) { return x < 1; }));
static_assert( some_of(w, [](int x) { return x < 2; }));
static_assert(!some_of(w, [](int x) { return x < 4; }));

int main()
{
std::vector<int> v(10, 2);
std::partial_sum(v.cbegin(), v.cend(), v.begin());
std::cout << "Among the numbers: ";
ranges::copy(v, std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';

if (ranges::any_of(v.cbegin(), v.cend(), [](int i) { return i % 2 == 0; }))
std::cout << "All numbers are even\n";

if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2)))
std::cout << "None of them are odd\n";

auto DivisibleBy = [](int d)
{
return [d](int m) { return m % d == 0; };
};

if (ranges::any_of(v, DivisibleBy(7)))
std::cout << "At least one number is divisible by 7\n";
}
输出
Among the numbers: 2 4 6 8 10 12 14 16 18 20
All numbers are even
None of them are odd
At least one number is divisible by 7
本文源自 此 CppReference 页面。它可能经过修改以进行改进或满足编辑偏好。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。

std::ranges::any_of() 算法

// (1)
constexpr bool any_of( I first, S last, Pred pred, Proj proj = {} );

// (2)
constexpr bool any_of( R&& r, Pred pred, Proj proj = {} );

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

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

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

检查谓词是否对范围内的任何元素都成立。

  • (1) 检查一元谓词 pred 是否对范围 [first; last) 中的任何元素(在通过投影 proj 投影后)返回 true

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

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

参数

first
last

要检查的元素范围。

r

要检查的元素范围。

proj

应用于元素的投影。

pred

应用于投影元素的谓词。

返回值

如果范围内至少有一个迭代器 i 使得 std::invoke(pred, std::invoke(proj, *i)) != false,则返回 true,否则返回 false

重要

如果范围为空,则返回 true

复杂度

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

异常

(无)

可能的实现

any_of(1) 和 any_of(2)
struct any_of_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 bool operator()(I first, S last, Pred pred, Proj proj = {}) const
{
return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) != last;
}

template<ranges::input_range R, class Proj = std::identity,
std::indirect_unary_predicate<
std::projected<ranges::iterator_t<R>,Proj>> Pred>
constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const
{
return operator()(ranges::begin(r), ranges::end(r),
std::ref(pred), std::ref(proj));
}
};

inline constexpr any_of_fn any_of;

示例

Main.cpp
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>

namespace ranges = std::ranges;

constexpr bool some_of(auto&& r, auto&& pred) // some but not any
{
return not (ranges::any_of(r, pred) or ranges::none_of(r, pred));
}

constexpr auto w = { 1, 2, 3 };
static_assert(!some_of(w, [](int x) { return x < 1; }));
static_assert( some_of(w, [](int x) { return x < 2; }));
static_assert(!some_of(w, [](int x) { return x < 4; }));

int main()
{
std::vector<int> v(10, 2);
std::partial_sum(v.cbegin(), v.cend(), v.begin());
std::cout << "Among the numbers: ";
ranges::copy(v, std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';

if (ranges::any_of(v.cbegin(), v.cend(), [](int i) { return i % 2 == 0; }))
std::cout << "All numbers are even\n";

if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2)))
std::cout << "None of them are odd\n";

auto DivisibleBy = [](int d)
{
return [d](int m) { return m % d == 0; };
};

if (ranges::any_of(v, DivisibleBy(7)))
std::cout << "At least one number is divisible by 7\n";
}
输出
Among the numbers: 2 4 6 8 10 12 14 16 18 20
All numbers are even
None of them are odd
At least one number is divisible by 7
本文源自 此 CppReference 页面。它可能经过修改以进行改进或满足编辑偏好。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。