Jump to content
  • Advertisement

Archived

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

aaron_ds

linked list: find first where (blah, compares, blah)

This topic is 5278 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

In the course of writing code for my current project, I''ve come across the need/want for a generic function that returns from a linked list a class that has one member equal/lessthan/greaterthan a reference value. For instance, if I had a list of 3 dimensional points and wanted to return the first point in the list where x==0. The function will iterate through the list testing if the condition is true and return the first object that passes. I''m struggling with what to pass into this function ''findfirstwhere''. I tried passing a pointer to a data member, but I dont think thats what I need. template template T* LINKED_LIST::findfirstwhere(U* a, const char& op, const U& b) is what I have so far, but I think its wayyyy off. I can do this without using a function, BUT having a generic function is much more appealing/streamlined. If anyone can point me in the right direction or paradigm, I would be much obliged.

Share this post


Link to post
Share on other sites
Advertisement
find_if

has example:


list<int> L;
L.push_back(-3);
L.push_back(0);
L.push_back(3);
L.push_back(-2);

list<int>::iterator result = find_if(L.begin(), L.end(),
bind2nd(greater<int>(), 0));
assert(result == L.end() || *result > 0);

Share this post


Link to post
Share on other sites
Source for MSVC++6''s version of find_if:
template<class _II, class _Pr> inline
_II find_if(_II _F, _II _L, _Pr _P)
{for (; _F != _L; ++_F)
if (_P(*_F))
break;
return (_F); }
Transated into something a bit more readable:
template <class t_Iterator, class t_Predicate>
inline t_Iterator find_if(t_Iterator First, t_Iterator Last, t_Predicate Predicate)
{
while (First != Last)
{
if (Predicate(*First))
break;
++First;
}
return First;
}
You should probably be able to convert that fairly easily to work with your own linked list.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!