Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Stoffel

std::find_if challenge

This topic is 5928 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Maybe find_if isn''t the right function to be using, but here goes:
  
class Event
{
public:
   int getType () const { return m_type; }
private:
   int m_type;
};
  
I want to take a container (say a vector) of the above type, and use find_if to find the first element whose getType returns 42. I think these functions need to be used: find_if mem_fun_ref equal_to bind1st or bind2nd ..but I have no idea how to put it all together. Anybody have a clue?

Share this post


Link to post
Share on other sites
Advertisement
I don''t know if this is the ''nicest'' way, but this is what I''ve been doing for situations like that:

  
class Event
{
public:
int getType () const { return m_type; }
private:
int m_type;
};

// ...


#include <list>
#include <algorithm>

struct FindIfEventTypeIs {
int Type;

inline bool operator () (const Event &e) {
return e.getType() == Type;
}

FindIfEventTypeIs(int pType) : Type(pType) {
}
};

std::list <Event> EventList;

// ...


std::list <Event>::iterator Result = std::find_if(EventList.begin(),EventList.end(),FindIfEventTypeIs(10));

But, if someone has an example that better exploits the STL, please show me too .

Share this post


Link to post
Share on other sites
Could we add a condition to the challenge? Answer without looking at a reference. After all, it''s quite easy to write your own predicate. STL masters (if that exists) shouldn''t have to look it up. The whole point of using those predicates is to become a faster coder.

Cédric

Share this post


Link to post
Share on other sites
quote:
Original post by cedricl
Could we add a condition to the challenge? Answer without looking at a reference. After all, it''s quite easy to write your own predicate. STL masters (if that exists) shouldn''t have to look it up. The whole point of using those predicates is to become a faster coder.

Cédric



Err... I disagree. Using a reference manual or online help at least lets you check the correctness of your knowledge. Besides, STL is complex enough that if you don''t know what you''re looking for, you''ll probably never find it.

MSN

Share this post


Link to post
Share on other sites
No, I actually want an answer to this, so please do look at the reference.

I should have stipulated: I'd like this done using only the standard adapters, or those in boost. No creating your own predicate.

[edited by - Stoffel on September 16, 2002 4:30:56 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Stoffel
I want to take a container (say a vector) of the above type, and use find_if to find the first element whose getType returns 42. I think these functions need to be used:
find_if
mem_fun_ref
equal_to
bind1st or bind2nd

..but I have no idea how to put it all together. Anybody have a clue?


I use boost\bind to get the correct behavior. bind1st and bind2nd only work with values.

MSN

Share this post


Link to post
Share on other sites
The functor is a good solution. Any reason why you don't want to use one?

[edited by - Zipster on September 16, 2002 4:33:34 PM]

Share this post


Link to post
Share on other sites
I've got a solution:

    
struct pred{static bool f(Event &e){return e.getType() == 42;}};
vector<Event>::iterator it = find_if(v.begin(), v.end(), pred::f);

And I didn't have to look at a reference. By the time MSDN is open on your computer, my code is already working.

My point is that I very rarely use less or equal_to with the standard algorithms, and I'm curious to know anyone who does. If you have to look them up every time you use them, then there is no advantage to using them, IMO.

Isn't boost::lambda's purpose to solve these problems, and to introduce a 'natural' syntax for these predicates?

Cédric

EDIT: Stoffel, sorry, I had not seen your last message, and now that I look at it, this is almost a thread hi-jacking for my rant about the STL predicates. I shut up now.

[edited by - cedricl on September 16, 2002 4:43:56 PM]

Share this post


Link to post
Share on other sites

      
#include <vector>

#include <algorithm>

#include <functional>

using namespace std;


class Event
{
public:
int getType () const { return m_type; }
private:
int m_type;
};

int main()
{
vector<Event> v;

v.push_back(Event());
v.push_back(Event());

find_if(v.begin(), v.end(),
compose1(bind2nd(equal_to<int>(),42),mem_fun_ref(&Event::getType)));

}


[edited by - SabreMan on September 16, 2002 4:50:37 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Stoffel
No, I actually want an answer to this, so please do look at the reference.

I should have stipulated: I''d like this done using only the standard adapters, or those in boost. No creating your own predicate.




  

std::find_if(v.begin(), v.end(),
boost::bind<bool>(std::equal_to<int>(), 42,
boost::bind(&Event::getType, _1)
),
);



At least it compiles correctly.

MSN

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!