根据值找键值模板类

This commit is contained in:
lovelyyoung 2020-06-20 11:09:54 +08:00
parent c92855206e
commit 185fcf5a7d
1 changed files with 16 additions and 0 deletions

16
huagao/MapFinder.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include <algorithm>
template<typename MapType>
auto get_map_key_by_value(const MapType& input_map, const decltype(input_map.begin()->second)& mapped_value) -> decltype(input_map.begin()->first)
{
auto iter = std::find_if(input_map.begin(), input_map.end(), [mapped_value](const auto& item) {
return (item.second == mapped_value);
});
if (iter == input_map.end())
{
return decltype(input_map.begin()->first)();
}
return iter->first;
}