Say I have an std::vector<int>
and want to know if it contains a 3
or get the iterator to the 3
.
I do not want to use an std::set
or std::multiset
for whatever reason.
I would like to do this in std::execution::par_unseq
mode.
The two options I see are std::any_of
and std::find
, but they do not quite do it for me.
#include <execution>
#include <functional>
#include <iostream>
#include <vector>
int main()
{
std::vector vec{ 1, 1, 1, 1, 1, 3, 3, 3, 3 };
bool contains{ std::any_of(
std::execution::par_unseq,
vec.begin(), vec.end(),
std::bind(std::equal_to{}, std::placeholders::_1, 3)) };
auto found{ std::find(std::execution::par_unseq, vec.begin(), vec.end(), 3) };
return 0;
}
The std::any_of
should do what I want, but the call is extremely messy for what it does. Ranges and std::bind_front
will help, but not a whole lot.
The problem with std::find
is that it has to find the first occurrence of a 3
, which limits its efficiency, as I do not care which 3
it finds.
- Is there an alternative to
std::any_of
that searches by value? - Is there
std::find
(andstd::search
) that finds any match, not the first?
Answers up to c++20 are welcome.
EDIT:
For clarification, I do not want a function that checks for contains and gives me an iterator at the same time. I am looking for two distinct functions (One that returns an iterator, one that returns a bool).