Skip to content

Instantly share code, notes, and snippets.

@kazmura11
Created December 31, 2016 20:45
Show Gist options
  • Save kazmura11/bd484dfb1f819d1b59654a73dff5ce78 to your computer and use it in GitHub Desktop.
Save kazmura11/bd484dfb1f819d1b59654a73dff5ce78 to your computer and use it in GitHub Desktop.
mapの最小値を求める
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <iostream>
auto main()->int
{
std::ios::sync_with_stdio(false);
std::unordered_map<std::string, int> m {{"a",1},{"b",2},{"c",3},{"d",4}};
// valueの最小値を求める
auto it = std::min_element(m.begin(), m.end(),
[](decltype(m)::value_type& l, decltype(m)::value_type& r)
-> bool { return l.second < r.second; });
// hashなので順序はぐちゃぐちゃです。
for (const auto& i : m)
{
std::cout << "{" << i.first << ":" << i.second << "},"; // => ぐちゃぐちゃ {b:2},{d:4},{c:3},{a:1},
}
std::cout << '\n';
std::cout << "{" << it->first << ":" << it->second << "}" << std::endl; // => {a,1}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment