Skip to content

Instantly share code, notes, and snippets.

@hrafnkelle
Created May 8, 2012 22:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrafnkelle/2639849 to your computer and use it in GitHub Desktop.
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 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