| | |
STL find_if
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
C Syntax (Toggle Plain Text)
#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) ) ; }
![]() |
Similar Threads
- Creating STL files From vertex and polygon data created in IDL (Computer Science)
- STL set vs. map (C++)
- STL on Solaris with Forte 7.0 compiler (C++)
- Using the STL LIst Container, how do I create, write,read, and store in file. (C++)
- sorting stl::map (C++)
- using(STL)function object (bind2nd) with a user defined function object (C++)
Other Threads in the C Forum
- Previous Thread: Need 4 destructors !!
- Next Thread: about classes, give a hand
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation testing threads unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






