. functor C std::find_if C.
:
#include "stdafx.h"
#include <vector>
#include <cassert>
#include <algorithm>
#include <iostream>
class C
{
private:
struct Foo
{
int key1, key2, value;
};
std::vector<Foo> fooList;
struct Finder
{
private:
int key1, key2;
public:
Finder(int k1, int k2)
{
key1 = k1;
key2 = k2;
}
bool operator ()(Foo const& foo) const
{
return foo.key1 == key1 || foo.key2 == key2;
}
};
public:
C()
{
Foo foo1, foo2;
foo1.key1 = 5;
foo1.key2 = 6;
foo1.value = 1;
foo2.key1 = 7;
foo2.key2 = 8;
foo2.value = 10;
fooList.insert(fooList.begin(), foo1);
fooList.insert(fooList.begin(), foo2);
}
int Find(int key1, int key2)
{
return std::find_if(fooList.begin(), fooList.end(), Finder(key1, key2))->value;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
C c;
std::cout << c.Find(5, 3) << std::endl;
std::cout << c.Find(3, 6) << std::endl;
std::cout << c.Find(7, 3) << std::endl;
std::cout << c.Find(3, 8) << std::endl;
return 0;
}
MaxGuernseyIII