I have an std::unordered_map<int, int> which stores the frequency count of each element present in a given array. I need to find the max frequency element and print the key and frequency count.
#include <iostream> #include <unordered_map> #include <type_traits> int main() { std::unordered_map<int, int> mp { { 1, 2 }, { 2, 54 }, { 3, 32 }, { 4, 8 }, { 5, 56 }, { 6, 23 }, { 7, 9 }, { 8, 87 }, { 9, 69 }, }; auto maxP = std::ref(*mp.begin()); for (const auto& p : mp) { if (p.second > maxP.get().second) maxP = std::ref(std::add_lvalue_reference<std::pair<const int, int>&>(std::remove_const<const std::pair<const int, int>>(std::remove_reference<const std::pair<const int, int>&>(p)))); } std::cout << maxP.get().first << ", " << maxP.get().second << std::endl; } But I'm getting the below error
<Main.cpp>:21:191: error: no matching function for call to 'std::remove_reference<const std::pair<const int, int>&>::remove_reference(const std::pair<const int, int>&)' 21 | maxP = std::ref(std::add_lvalue_reference<std::pair<const int, int>&>(std::remove_const<const std::pair<const int, int>>(std::remove_reference<const std::pair<const int, int>&>(p)))); |