Created
May 8, 2012 22:14
-
-
Save hrafnkelle/2639849 to your computer and use it in GitHub Desktop.
This is an attempt to learn how to use both find_if and C++ lambda functions to find an element in a collection using an anonymous predictate
This file contains 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
// This can be compiled with a recent g++ (version > 4.5 I belive) by issuing | |
// $ g++ -std=c++0x -o foo find_if_test.cpp | |
#include<iostream> | |
#include<vector> | |
#include<algorithm> | |
struct Record | |
{ | |
int a; | |
int b; | |
Record(int a, int b): a(a), b(b) {} | |
}; | |
int main() | |
{ | |
std::vector<Record> records; | |
records.push_back(Record(1,2)); | |
records.push_back(Record(2,3)); | |
records.push_back(Record(3,4)); | |
records.push_back(Record(4,5)); | |
int recordSumThreshold = 6; | |
std::vector<Record>::iterator itr = std::find_if(records.begin(), records.end(), [recordSumThreshold](Record r){return (r.a+r.b)>recordSumThreshold;}); | |
std::cout << "Found Record(" << itr->a << ", " << itr->b << ")" << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment