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 50 51 52 53 54 55 56 57 58 59
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> //for find_if
using namespace std;
struct Patient
{
int id;
string name;
int days_in;
Patient(int id_,const char * name_,int days_in_):
id(id_),name(name_),days_in(days_in_){}
struct HasId
{
int id;
HasId(int id_):id(id_){}
bool operator()(const Patient & p)
{
return p.id==id;
}
};
struct HasName
{
string name;
HasName(string name_):name(name_){}
bool operator()(const Patient & p)
{
return p.name==name;
}
};
};
int main()
{
vector<Patient> clinic;
clinic.push_back(Patient(1,"Bob",2));
clinic.push_back(Patient(2,"Tom",5));
clinic.push_back(Patient(3,"Pam",12));
clinic.push_back(Patient(4,"Sam",4));
clinic.push_back(Patient(5,"Liz",7));
//let's see the name of the patient who has an id of 3
cout << find_if(clinic.begin(),clinic.end(),Patient::HasId(3))->name << endl;
//let's see the id of the patient named "Liz"
cout << find_if(clinic.begin(),clinic.end(),Patient::HasName("Liz"))->id << endl;
cin.get();
return 0;
}
|