I wrote up a very sloppy solution. I didn't use any functors in mine though. They were given me some beef with v$ so I just did it with a standard compare function. Here is my sample..
Code:
#include <functional>
#include <algorithm>
#include <utility>
#include <iostream>
#include <string>
#include <deque>
// Functor
// Not used in my function..
class Search : public std::binary_function<std::pair<std::string, int>, std::string, bool>
{
public:
bool operator()(std::pair<std::string, int> &lp, std::string &rp)
{
return true;
}
};
typedef std::deque<std::pair<std::string, int> > deqStringInt;
typedef struct std::pair<std::string, int> PAIR;
// Here is my comparison function
bool simple( std::pair< std::string, int > lp, std::string rp )
{
return !strcmp( lp.first.c_str(), rp.c_str() );
}
int main( void )
{
deqStringInt example;
example.push_back( PAIR("December", 21) );
// How do you search an element?
deqStringInt::iterator i = std::find_if( example.begin(),
example.end(),
std::bind2nd( std::ptr_fun(simple), "December" ) );
if( i != example.end() )
{
std::cout << "Found" << std::endl;
}
else
{
std::cout << "Not Found :(" << std::endl;
}
return 0;
}
Tomorrow when I get some sleep I will post up the other version using functors if you'd like.