Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions sort_map_by_value_ascending.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <map>
#include <set>
#include <algorithm>
#include <functional>
#include <iostream>

int main()
{

// Creating & Initializing a map of String & Ints
std::map<std::string, int> mapOfWordCount = { { "a", 10 }, { "b", 41 }, { "c", 62 }, { "d", 13 } };

// Declaring the type of Predicate that accepts 2 pairs and return a bool
typedef std::function<bool(std::pair<std::string, int>, std::pair<std::string, int>)> Comparator;

// Defining a lambda function to compare two pairs. It will compare two pairs using second field
Comparator compFunctor =
[](std::pair<std::string, int> element1 ,std::pair<std::string, int> element2)
{
return element1.second < element2.second;
};

// Declaring a set that will store the pairs using above comparision logic
std::set<std::pair<std::string, int>, Comparator> setOfWords(mapOfWordCount.begin(), mapOfWordCount.end(), compFunctor);

// Iterate over a set
// It will display the items in sorted order of values
for (std::pair<std::string, int> element : setOfWords)
std::cout << element.first << " :: " << element.second << std::endl;

return 0;
}