Last active
July 19, 2016 15:47
-
-
Save mdauphin/f1156b585bdb71b2742fbb4633bf9db3 to your computer and use it in GitHub Desktop.
STL Find in object vector with attribute getter name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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