Last active
May 26, 2019 00:53
-
-
Save christianparpart/ab9298e897ac71cd472c26ca4a7ffa2d to your computer and use it in GitHub Desktop.
Getting the minimum element of a map<>.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <map> | |
| #include <string> | |
| #include <algorithm> | |
| #include <iostream> | |
| using namespace std; | |
| int main(int argc, const char* argv[]) | |
| { | |
| // hint: ternary operator ? : | |
| string const text = | |
| argc == 2 | |
| ? argv[1] | |
| : "Hello bello, mello yellow"; | |
| map<char, unsigned> freq; | |
| for (char c : text) | |
| ++freq[c]; | |
| auto isLess = [](const auto& a, const auto& b) -> bool { | |
| return a.second < b.second; | |
| } | |
| auto k = min_element(begin(freq), end(freq), isLess); | |
| auto i = min_element(begin(freq), end(freq), [](const auto& a, const auto& b) -> bool { | |
| return a.second < b.second; | |
| }); | |
| if (i != end(freq)) | |
| cout << "Minimum element: " << i->first << " with count " << i->second << '\n'; | |
| else | |
| cout << "No minimum found.\n"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!