By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

using std::find

P: n/a
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?

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.

Mar 14 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 14 '07 #2

P: n/a
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<MyClassv;

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))_;
Mar 14 '07 #3

P: n/a
red floyd wrote:
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<MyClassv;

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))_;
I couldn't find the word "remove" in the original post. Only "get"
and "find". For that 'find' is called in a 'while' loop and the
beginning of the range should keep changing:

it = c.begin();
while (true) {
it = std::find(it, c.end(), hasElement(42));
if (it != c.end()) {
// do something with 'it' or with '*it' or with it->x or...
}
else
break;
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 14 '07 #4

P: n/a
On 14 Mar 2007 10:48:40 -0700 in comp.lang.c++, "brekehan"
<cp***@austin.rr.comwrote,
>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?
I think you might end up using std::find_if() with a predicate that
selects the values you want. The predicate could be a boost::lambda
expression, or it could be hand crafted. Since find() and find_if()
return just one thing, you could loop to find them all.

Some random fragments:

bool ifx(MyClass const & c, int x)
{ return c.x == x; }

for (iterator_type it = container.begin(); it != container.end();
it = find_if(it, container.end(), std::bind_2nd(ifx, om))) {
if (it != container.end())
cout << *it++;
}

Mar 14 '07 #5

P: n/a
On Mar 14, 12:53 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

om = some. this keyboard sucks. Sorry. I also used to have three
really good STL references, but the girlfriend got angry that I love
the computer more than her and disposed of most of my office. I plan
on getting some more, meantime I got a deadline to meet.

I see that a few of the functions in <algorithmor the container
itself take the functor argument you mentioned, but if I understand
correctly this is passed 1 argument of the type contained. remove_if
would be awesome, because that is exatly what my goal is. Remove an
element if it has a member (we'll call it TYPE m_type) of some type.
However, I need to have two arguments...the element and the TYPE to
compare it with, so my functor can say is this element of this type?
Otherwise, I have to write a functor for every possible value of
m_type and there is no telling how many there may be in the future.

Mar 14 '07 #6

P: n/a
On 14 Mar 2007 11:35:52 -0700 in comp.lang.c++, "brekehan"
<cp***@austin.rr.comwrote,
>However, I need to have two arguments...the element and the TYPE to
compare it with, so my functor can say is this element of this type?
Otherwise, I have to write a functor for every possible value of
m_type and there is no telling how many there may be in the future.
Solving that part of the problem is the job of the std::bind* family.
Or else something more modern and powerful like boost::bind.

Mar 14 '07 #7

P: n/a
Victor Bazarov wrote:
red floyd wrote:
>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<MyClassv;

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))_;

I couldn't find the word "remove" in the original post. Only "get"
and "find". For that 'find' is called in a 'while' loop and the
beginning of the range should keep changing:

it = c.begin();
while (true) {
it = std::find(it, c.end(), hasElement(42));
if (it != c.end()) {
// do something with 'it' or with '*it' or with it->x or...
}
else
break;
}
I thought remove_copy_if doesn't modify the original container. The
construct I gave should copy all elements with x == 42.

Mar 14 '07 #8

P: n/a
brekehan <cp***@austin.rr.comwrote:
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?
I believe std::find by default uses std::less<T>, which by default uses
operator<. So, you can provide your own operator<, or explicitly
provide a comparison functor to find().

Then, one way would be to sort your container, then use std::lower_bound
and std::upper_bound.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 14 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.