(C++) GetKeyWithMaxValue

February 24, 2017 · View on GitHub

 

 

 

 

 

(C++) GetKeyWithMaxValue

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • STL STL: GNU ISO C++ Library, version 4.9.2

 

 

 

 

 

Qt project file: ./CppGetKeyWithMaxValue/CppGetKeyWithMaxValue.pro

 


include(../../ConsoleApplication.pri) #Or use the code below # QT += core # QT += gui # greaterThan(QT_MAJOR_VERSION, 4): QT += widgets # CONFIG   += console # CONFIG   -= app_bundle # TEMPLATE = app # CONFIG(release, debug|release) { #   DEFINES += NDEBUG NTRACE_BILDERBIKKEL # } # QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ # unix { #   QMAKE_CXXFLAGS += -Werror # } include(../../Libraries/Boost.pri) #Or use the code below # win32 { #   INCLUDEPATH += \ #     ../../Libraries/boost_1_54_0 # } SOURCES += main.cpp

 

 

 

 

 

./CppGetKeyWithMaxValue/main.cpp

 


#include <algorithm> #include <cassert> #include <map> #include <iostream> #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp> #pragma GCC diagnostic pop ///Obtain the key that corresponds to the highest value //From http://www.richelbilderbeek.nl/CppGetKeyWithMaxValue.htm template <class Key, class Value> const Key GetKeyWithMaxValue(const std::map<Key,Value>& v) {   assert(!v.empty());   return std::max_element(     v.begin(),v.end(),     boost::lambda::bind(&std::pair<Key,Value>::second, boost::lambda::_2)      > boost::lambda::bind(&std::pair<Key,Value>::second, boost::lambda::_1)    )->first; } int main() {   std::map<std::string,int> m;   m.insert(std::make_pair("eight",8));   m.insert(std::make_pair("four",4));   m.insert(std::make_pair("one",1));   m.insert(std::make_pair("sixteen",16));   m.insert(std::make_pair("two",2));   assert(GetKeyWithMaxValue(m) == "sixteen"); }