std::experi...es::any_of,(3)
okay bestie, we're in the C++ Standard Libary manual and today's page is std::experi...es::any_of,(3) β yes, the title is doing the absolute most, but we're gonna get through it together π
π NAME π
std::experimental::ranges::all_of,std::experimental::ranges::any_of, - std::experimental::ranges::all_of,std::experimental::ranges::any_of,
periodt. that's the name section, and it's chaotic but iconic.
β¨ Synopsis β¨
okay so this is defined in the header <experimental/ranges/algorithm>, and the whole overload lineup is giving major ranges TS energy. here's the exact synopsis, bestie:
Defined in header <experimental/ranges/algorithm>
template< InputIterator I, Sentinel<I> S, class Proj =
ranges::identity,
(1)
(ranges TS)
IndirectUnaryPredicate<projected<I, Proj>> Pred >
bool all_of( I first, S last, Pred pred, Proj proj = Proj{} );
template< InputRange R, class Proj = ranges::identity,
IndirectUnaryPredicate<projected<ranges::iterator_t<R>,
(2) (ranges TS)
Proj>> Pred >
bool all_of( R&& r, Pred pred, Proj proj = Proj{} );
template< InputIterator I, Sentinel<I> S, class Proj =
ranges::identity,
(3)
(ranges TS)
IndirectUnaryPredicate<projected<I, Proj>> Pred >
bool any_of( I first, S last, Pred pred, Proj proj = Proj{} );
template< InputRange R, class Proj = ranges::identity,
IndirectUnaryPredicate<projected<ranges::iterator_t<R>,
(4) (ranges TS)
Proj>> Pred >
bool any_of( R&& r, Pred pred, Proj proj = Proj{} );
template< InputIterator I, Sentinel<I> S, class Proj = identity,
IndirectUnaryPredicate<projected<I, Proj>> Pred >
(5) (ranges TS)
bool none_of( I first, S last, Pred pred, Proj proj = Proj{} );
template< InputRange R, class Proj = ranges::identity,
IndirectUnaryPredicate<projected<ranges::iterator_t<R>,
(6) (ranges TS)
Proj>> Pred >
bool none_of( R&& r, Pred pred, Proj proj = Proj{} );
the little (1)β(6) tags are literally part of the synopsis, so don't lose them. same with every (ranges TS) note β they're lowkey important.
- Checks if unary predicate
predreturnstruefor all elements in the range[first, last). - Checks if unary predicate
predreturnstruefor at least one element in the range[first, last). - Checks if unary predicate
predreturnstruefor no elements in the range[first, last).
2,4,6) Same as (1,3,5), but uses r as the source range, as if using ranges::begin(r) as first and ranges::end(r) as last.
Notwithstanding the declarations depicted above, the actual number and order of template parameters for algorithm declarations is unspecified. Thus, if explicit template arguments are used when calling an algorithm, the program is probably non-portable.
πΈ Parameters πΈ
first,lastβ the range of the elements to examinerβ the range of the elements to examinepredβ predicate to apply to the projected elementsprojβ projection to apply to the elements
π¦ Return value π¦
1,2)trueifpredreturnstruefor all elements in the range,falseotherwise. Returnstrueif the range is empty.3,4)trueifpredreturnstruefor at least one element in the range,falseotherwise. Returnsfalseif the range is empty.5,6)trueifpredreturnstruefor no elements in the range,falseotherwise. Returnstrueif the range is empty.
π Complexity π
1-6) At most last - first applications of the predicate and last - first applications of the projection.
π§ Possible implementation π§
First version
template<InputIterator I, Sentinel<I> S, class Proj =
ranges::identity,
IndirectUnaryPredicate<projected<I, Proj>> Pred>
bool all_of(I first, S last, Pred pred, Proj proj = Proj{})
{
return ranges::find_if_not(first, last, std::ref(pred),
std::ref(proj)) == last;
}
template<InputRange R, class Proj = ranges::identity,
IndirectUnaryPredicate<projected<ranges::iterator_t<R>,
Proj>> Pred>
bool all_of(R&& r, Pred pred, Proj proj = Proj{})
{
return ranges::all_of(ranges::begin(r), ranges::end(r),
std::ref(pred), std::ref(proj));
}
Second version
template<InputIterator I, Sentinel<I> S, class Proj =
ranges::identity,
IndirectUnaryPredicate<projected<I, Proj>> Pred>
bool any_of(I first, S last, Pred pred, Proj proj = Proj{})
{
return ranges::find_if(first, last, std::ref(pred),
std::ref(proj)) != last;
}
template<InputRange R, class Proj = ranges::identity,
IndirectUnaryPredicate<projected<ranges::iterator_t<R>,
Proj>> Pred>
bool any_of(R&& r, Pred pred, Proj proj = Proj{})
{
return ranges::any_of(ranges::begin(r), ranges::end(r),
std::ref(pred), std::ref(proj));
}
Third version
template<InputIterator I, Sentinel<I> S, class Proj = identity,
IndirectUnaryPredicate<projected<I, Proj>> Pred>
bool none_of(I first, S last, Pred pred, Proj proj = Proj{})
{
return ranges::find_if(first, last, std::ref(pred),
std::ref(proj)) == last;
}
template<InputRange R, class Proj = ranges::identity,
IndirectUnaryPredicate<projected<ranges::iterator_t<R>,
Proj>> Pred>
bool none_of(R&& r, Pred pred, Proj proj = Proj{})
{
return ranges::none_of(ranges::begin(r), ranges::end(r),
std::ref(pred), std::ref(proj));
}
π Example π
// Run this code
#include <experimental/ranges/algorithm>
#include <experimental/ranges/iterator>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
namespace ranges = std::experimental::ranges;
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, ranges::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
if (ranges::all_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";
struct DivisibleBy
{
const int d;
DivisibleBy(int n) : d(n) {}
bool operator()(int n) const { return n % d == 0; }
};
if (ranges::any_of(v, DivisibleBy(7)))
std::cout << "At least one number is divisible by 7\n";
}
Output:
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
β See also β
all_ofany_ofnone_ofβ checks if a predicate is true for all, any or none of the elements in a range(C++11)(function template)(C++11)(C++11)
okay bye bestie π«Ά β source: http://cppreference.com, version/date: 2024.06.10, page: std::experi...es::any_of,(3) β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms