#include #include #include #include #include int main() { // Creating & Initializing a map of String & Ints std::map mapOfWordCount = { { "aaa", 10 }, { "ddd", 41 }, { "bbb", 62 }, { "ccc", 13 } }; // Declaring the type of Predicate that accepts 2 pairs and return a bool typedef std::function, std::pair)> Comparator; // Defining a lambda function to compare two pairs. It will compare two pairs using second field Comparator compFunctor = [](std::pair elem1 ,std::pair elem2) { return elem1.second < elem2.second; }; // Declaring a set that will store the pairs using above comparision logic std::set, Comparator> setOfWords( mapOfWordCount.begin(), mapOfWordCount.end(), compFunctor); // Iterate over a set using range base for loop // It will display the items in sorted order of values for (std::pair element : setOfWords) std::cout << element.first << " :: " << element.second << std::endl; return 0; }