943,553 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3705
  • C RSS
Mar 18th, 2007
0

STL find_if

Expand Post »
Hey everybody,
Could somebody please provide an example for using the STL find_if for the following task. I am trying to search a vector of structs, using the following criteria: compare whether the key of the struct in the vector matches the key of another struct (instace of the same type of struct) that I want to find. So basically search the vector for a particular struct. I want to use find_if, in order to replace the handwritten routine I have now. I have seen many complex, examples online. If anybody knows of simple and clean way, please share. Thanks in advance.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Posting Pro in Training
MIGSoft is offline Offline
472 posts
since Jul 2005
Mar 19th, 2007
0

Re: STL find_if

Loop through the vector of objects, if that object matches the one in hand return a success message?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 20th, 2007
0

Re: STL find_if

  1. #include <vector>
  2. #include <algorithm>
  3.  
  4. struct some_struct { int key ; /* ... */ };
  5.  
  6. struct has_same_key
  7. {
  8. explicit ( const some_struct& s ) : what(s) {}
  9. bool operator() ( const some_struct& elem ) const
  10. { return elem.key == what.key ; }
  11. const some_struct& what ;
  12. };
  13.  
  14. inline std::vector<some_struct>::const_iterator
  15. do_find( const std::vector<some_struct>& vec, const some_struct& what )
  16. {
  17. return std::find_if( vec.begin(), vec.end(), has_same_key(what) ) ;
  18. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Need 4 destructors !!
Next Thread in C Forum Timeline: about classes, give a hand





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC