1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <algorithm>
#include <string>
#include <iostream>
#include <cassert>
const unsigned int NUM_RECORDS(5);
struct record
{
unsigned char id;
std::string name;
std::string enrollDate;
};
struct CompareIds
{
// This is the constructor.
CompareIds(unsigned char id) : id_(id) {}
// This is the operator() which makes this class
// a functor. When used by an algorithm it will be
// invoked for every object of the container.
bool operator()(const record& rhs)
{
return (id_ == rhs.id);
}
// This attribute is stored during construction and is
// the value being searched for.
unsigned char id_;
};
int main()
{
record recordArray[NUM_RECORDS] =
{
0x1, "Shawn Thompson", "01/18/1974",
0x7f, "Davey Jones", "05/22/1978",
0x5, "Henry Forbes", "03/17/1988",
0x2, "Steven Penske", "04/01/1963",
0x62, "Felicia Simpson", "07/04/1976"
};
record* pos = 0;
record* end = recordArray + NUM_RECORDS;
pos = std::find_if(recordArray, end, CompareIds(0xff));
assert(pos == end);
pos = std::find_if(recordArray, end, CompareIds(0x62));
assert(pos != end);
return 0;
}
|