STL find_if

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2005
Posts: 472
Reputation: MIGSoft is an unknown quantity at this point 
Solved Threads: 1
MIGSoft's Avatar
MIGSoft MIGSoft is offline Offline
Posting Pro in Training

STL find_if

 
0
  #1
Mar 18th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: STL find_if

 
0
  #2
Mar 19th, 2007
Loop through the vector of objects, if that object matches the one in hand return a success message?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: STL find_if

 
0
  #3
Mar 20th, 2007
  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC