#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
struct Candy {
string name;
int weight;
Candy(string candyName, int candyWeight) {
name = candyName;
weight = candyWeight;
}
};
struct Child {
string name;
int requirement;
Child(string childName, int requirementWeight) {
name = childName;
requirement = requirementWeight;
}
};
void printChildCandyMap(std::map<string, string> *childCandy) {
cout << "Child" << ":" << "Candy" << endl;
for (std::map<string, string>::iterator i = childCandy->begin(); i != childCandy->end(); ++i)
{
cout << i->first << " : " << i->second << endl;
}
}
bool compareSmallerCandyWeight(Candy left, Candy right) {
return left.weight < right.weight;
}
bool compareSmallerChildRequirement(Child left, Child right) {
return left.requirement < right.requirement;
}
void handoutCandy(std::vector<Child> *children, std::vector<Candy> *candys, std::map<string, string> *childCandy) {
sort(children->begin(), children->end(), compareSmallerChildRequirement);
sort(candys->begin(), candys->end(), compareSmallerCandyWeight);
std::vector<Candy>::iterator candyIter = candys->begin();
std::vector<Child>::iterator childIter = children->begin();
while (candyIter != candys->end() && childIter != children->end())
{
if (candyIter->weight >= childIter->requirement) {
childCandy->insert(pair<string, string>(childIter->name + " " + to_string(childIter->requirement),
candyIter->name + " " + to_string(candyIter->weight)));
++childIter;
}
++candyIter;
}
}
int main(){
std::vector<Candy> candys;
std::vector<Child> children;
children.push_back(Child("Ross", 15));
children.push_back(Child("Lucy", 9));
children.push_back(Child("Jelly", 5));
children.push_back(Child("Mark", 9));
children.push_back(Child("Heather", 2));
children.push_back(Child("Mike", 10));
candys.push_back(Candy("Chocolate", 20));
candys.push_back(Candy("Chocolate", 6));
candys.push_back(Candy("Chocolate", 8));
candys.push_back(Candy("Chocolate", 3));
candys.push_back(Candy("Chocolate", 1));
std::map<string, string> childCandy;
handoutCandy(&children, &candys, &childCandy);
printChildCandyMap(&childCandy);
children.clear();
children.push_back(Child("Ross", 6));
children.push_back(Child("Lucy", 9));
children.push_back(Child("Jelly", 5));
children.push_back(Child("Mark", 7));
children.push_back(Child("Heather", 2));
children.push_back(Child("Mike", 10));
candys.clear();
candys.push_back(Candy("Chocolate", 10));
candys.push_back(Candy("Chocolate", 6));
candys.push_back(Candy("Chocolate", 8));
candys.push_back(Candy("Chocolate", 3));
candys.push_back(Candy("Chocolate", 1));
childCandy.clear();
handoutCandy(&children, &candys, &childCandy);
printChildCandyMap(&childCandy);
}