Re: using std::find
Victor Bazarov wrote:
brekehan wrote:
If I have a class
MyClass
{
...bunch o data and methods
int x;
};
and a stl container of MyClass objects
Is there a way to use std::find to get all the elements whose member x
is of om value?
"om" value? What's "om" value? Sorry, English is not my mother
tongue.
All the example on the net are of a collection of some built in type
for simplicity, but do not show how to use a member of your own type
as the value being sought.
What does your favourite C++ book say? Don't you have a copy of
Josuttis' "The C++ Standard Library"? You can define your own
functor to compare the value of the 'x' member with the given value
and then call
std::find(container.begin(), container.end(), yourFunctor(42));
V
I think he wants remove_copy_if() with negated functor?
std::vector<MyClass> v;
struct hasElement: public std::unary_function<MyClass, bool>
{
bool operator()(const MyClass& elem) const
{
return elem.x == x;
}
hasElement(int x_) : x(x_) { }
private
int x;
};
std::remove_copy_if(container.begin(), container.end(),
std::back_inserter(v),
std::not(hasElement(42))_;