(C++) Answer of exercise \#9: No for-loops \#30
August 30, 2019 · View on GitHub
This is the answer of Exercise #9: No for-loops.
Question #30: Get maximum value from std::map<const Person *,int>
Replace the for-loop. You will need:
#include <cassert> #include <limits> #include <map> #include <boost/foreach.hpp> struct Person { }; const Person * GetPersonWithMaxIdStl(const std::map<const Person *,int>& v) { assert(!v.empty()); int max_id = std::numeric_limits<int>::min(); const Person * ptr = 0; typedef std::pair<const Person *,int> Pair; BOOST_FOREACH(const Pair& p,v) { if (p.second > max_id) { max_id = p.second; ptr = p.first; } } assert(ptr); return ptr; }
Answer using Boost.Bind
#include <algorithm> #include <cassert> #include <map> #include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp> struct Person {}; const Person * GetPersonWithMaxId(const std::map<const Person *,int>& v) { assert(!v.empty()); return std::max_element( v.begin(),v.end(), boost::lambda::bind(&std::pair<const Person *,int>::second, boost::lambda::_2) > boost::lambda::bind(&std::pair<const Person *,int>::second, boost::lambda::_1) )->first; }