[NO-PARSE]#include <iostream>
#include <map>
bool keyComp(unsigned lch, unsigned rch) { return lch > rch; }
typedef std::pair < std::string, size_t > coin_def;
typedef std::map<size_t, coin_def, bool(*)(size_t, size_t)> coin_map;
const size_t TOONIE = 200;
const size_t LOONIE = 100;
const size_t QUARTER = 25;
const size_t DIME = 10;
const size_t NICKEL = 5;
const size_t PENNY = 1;
void CoinsRequired(double amt, coin_map& coinMap)
{
size_t pennySum = amt * 100;
for (const auto &it : coinMap)
{
size_t count = 0;
while (pennySum >= count + it.first)
{
count += it.first;
coinMap[it.first].second++;
}
pennySum -= count;
}
}
int main(void)
{
double amt = 2.37;
coin_map coinMap(keyComp);
coinMap.insert(std::pair < size_t, coin_def >(QUARTER, coin_def("Quarter(s)", 0)));
coinMap.insert(std::pair < size_t, coin_def >(DIME, coin_def("Dime(s)", 0)));
coinMap.insert(std::pair < size_t, coin_def >(NICKEL, coin_def("Nickel(s)", 0)));
coinMap.insert(std::pair < size_t, coin_def >(PENNY, coin_def("Penny/Pennies", 0)));
CoinsRequired(amt, coinMap);
// Output resules
std::cout << "Minimum Coin Sums for: $" << amt << std::endl;
for (const auto &it : coinMap)
{
std::cout.width(15);
std::cout << std::left << it.second.first << ": ";
std::cout << std::right << it.second.second << std::endl;
}
}[/NO-PARSE]