Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > using std::find

Reply
Thread Tools

using std::find

 
 
brekehan
Guest
Posts: n/a
 
      03-14-2007
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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-14-2007
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


 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      03-14-2007
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))_;


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-14-2007
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<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))_;


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


 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      03-14-2007
On 14 Mar 2007 10:48:40 -0700 in comp.lang.c++, "brekehan"
<(E-Mail Removed)> 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?


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++;
}

 
Reply With Quote
 
brekehan
Guest
Posts: n/a
 
      03-14-2007
On Mar 14, 12:53 pm, "Victor Bazarov" <(E-Mail Removed)> 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
> --
> 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 <algorithm> or 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.

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      03-14-2007
On 14 Mar 2007 11:35:52 -0700 in comp.lang.c++, "brekehan"
<(E-Mail Removed)> wrote,
>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.

 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      03-14-2007
brekehan <(E-Mail Removed)> 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?


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
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      03-14-2007
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<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))_;

>
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a Link Button to redirect to another page by using data from =?Utf-8?B?R1REcml2ZXI=?= ASP .Net 1 02-16-2005 07:04 PM
Error page using the Application_Error void dosnt work when using DIV for a please wait message s_erez@hotmail.com ASP .Net 2 12-24-2004 12:11 PM
no code in webform using vs.net, but in webform using notepad timmso ASP .Net 1 12-12-2003 04:30 PM
Using GetOleDbSchemaTable to get SQL Server Field Description - using pete ASP .Net 1 08-29-2003 10:50 AM
Re: MVP? Index error on nested element using System.xml but NOT using msxml??? William F. Robertson, Jr. ASP .Net 1 06-25-2003 08:08 PM



Advertisments