Skip to content

Instantly share code, notes, and snippets.

@mdauphin
Last active July 19, 2016 15:47
Show Gist options
  • Save mdauphin/f1156b585bdb71b2742fbb4633bf9db3 to your computer and use it in GitHub Desktop.
Save mdauphin/f1156b585bdb71b2742fbb4633bf9db3 to your computer and use it in GitHub Desktop.
STL Find in object vector with attribute getter name
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std ;
class A
{
string name ;
public:
A(const string& name) : name(name) {}
const string& getName() const { return name; }
};
int main()
{
vector<A> vec ;
vec.push_back( A("123") );
vec.push_back( A("456") );
vector<A>::iterator it = find_if( vec.begin(), vec.end(), tr1::bind(
std::equal_to<string>(),
tr1::bind( &A::getName, tr1::placeholders::_1 ),
"1234"
) );
cout << (it != vec.end()) << endl ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment