RSS Forums RSS
Please support our C advertiser: Programming Forums
Views: 1610 | Replies: 2
Reply
Join Date: Jul 2005
Posts: 472
Reputation: MIGSoft is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
MIGSoft's Avatar
MIGSoft MIGSoft is offline Offline
Posting Pro in Training

STL find_if

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: STL find_if

  #2  
Mar 19th, 2007
Loop through the vector of objects, if that object matches the one in hand return a success message?
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,087
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 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: STL find_if

  #3  
Mar 20th, 2007
#include <vector>
#include <algorithm>

struct some_struct { int key ; /* ... */ };

struct has_same_key
{
  explicit ( const some_struct& s ) : what(s) {}
  bool operator() ( const some_struct& elem ) const
  { return elem.key == what.key ; }
  const some_struct& what ;
};

inline std::vector<some_struct>::const_iterator 
   do_find( const std::vector<some_struct>& vec, const some_struct& what )
{
  return std::find_if( vec.begin(), vec.end(), has_same_key(what) ) ;
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:52 pm.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC