From ddd292b15c9e47a9d39c192800a6fe823c4e1f41 Mon Sep 17 00:00:00 2001 From: Lakshay Agarwal Date: Thu, 17 Oct 2019 18:53:02 +0530 Subject: [PATCH 1/2] Added question --- sort_map_by_value_ascending.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 sort_map_by_value_ascending.cpp diff --git a/sort_map_by_value_ascending.cpp b/sort_map_by_value_ascending.cpp new file mode 100644 index 0000000..17267c6 --- /dev/null +++ b/sort_map_by_value_ascending.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +#include + +int main() +{ + + // Creating & Initializing a map of String & Ints + std::map 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, std::pair)> Comparator; + + // Defining a lambda function to compare two pairs. It will compare two pairs using second field + Comparator compFunctor = + [](std::pair element1 ,std::pair element2) + { + return element1.second < element2.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 + // 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; +} \ No newline at end of file From c27cd606ed3c480c996c0a8750478bb87e8ca82f Mon Sep 17 00:00:00 2001 From: Lakshay-agarwal <32712374+Lakshay-agarwal@users.noreply.github.com> Date: Thu, 17 Oct 2019 18:55:50 +0530 Subject: [PATCH 2/2] Update sort_map_by_value_ascending.cpp --- sort_map_by_value_ascending.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sort_map_by_value_ascending.cpp b/sort_map_by_value_ascending.cpp index 17267c6..9081911 100644 --- a/sort_map_by_value_ascending.cpp +++ b/sort_map_by_value_ascending.cpp @@ -2,6 +2,7 @@ #include #include #include +#include int main() { @@ -28,4 +29,4 @@ int main() std::cout << element.first << " :: " << element.second << std::endl; return 0; -} \ No newline at end of file +}