Skip to content

Instantly share code, notes, and snippets.

@christianparpart
Last active May 26, 2019 00:53
Show Gist options
  • Select an option

  • Save christianparpart/ab9298e897ac71cd472c26ca4a7ffa2d to your computer and use it in GitHub Desktop.

Select an option

Save christianparpart/ab9298e897ac71cd472c26ca4a7ffa2d to your computer and use it in GitHub Desktop.
Getting the minimum element of a map<>.
#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";
}
@keithoma

Copy link
Copy Markdown

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment