Я новичок здесь, а также в шаблонном программировании. У меня есть словарь (значит, это может быть либо std::map
, либо std::vector<std::pair<type1, type2>>
, либо std::set<std::pair<, >
> ...). Я хотел написать алгоритм, который действует как стандартные библиотечные алгоритмы, используя итераторы переданного контейнера.
Ниже приводится идея.
#include <iostream>
#include <algorithm>
#include <type_traits>
#include <vector>
#include <array>
#include <map>
#include <set>
#include <iterator>
// two different types
enum EnumA { one, two, three, four, five, six};
enum EnumB { one, three, four, six};
// TypeA TypeB
using map = std::map<EnumA, EnumB>;
// or std::vector<std::pair<EnumA, EnumB>>
// or std::set<std::pair<EnumA, EnumB>>
// or std::array<std::pair<EnumA, EnumB>, 3>
const map itemMap{ // the map
{EnumA::one, EnumB::one},
{EnumA::three, EnumB::three},
{EnumA::six, EnumB::six},
};
template<typename Iterator, typename B>
/* type of KEY(first) of the map/container from the iterator???*/ AfromB(Iterator begin, Iterator end, B bObj)
{
// static_assert(begin != end); // container should not be empty!
using Type = typename std::iterator_traits<Iterator>::value_type;
using AType = decltype( /* how to find the type of KEY(first) of the map/container? */);
using BType = decltype(/* how to find the type of VALUE(second) of the map/container? */);
auto iter = std::find_if(begin, end, [bObj](const Type& entry) { return entry.second == bObj;});
return iter != end ? iter->first: begin->first; // if not found return the first element match
}
// will do BfromA(Iterator begin, Iterator end, B bObj) similarly afterwards
int main()
{
EnumA aEnum = AfromB(itemMap.cbegin(), itemMap.cend(), EnumB::six); // I can use it like
}
Там вы можете видеть в коде, я не знаю, как найти тип ключа / первого и значения / второго пары в словаре. После поиска в Google я обнаружил, что могу найти тип пары ключ-значение по
using Type = typename std::iterator_traits<Iterator>::value_type;
, но не для лиц этой пары. Можно ли найти? Я использую C ++ 11.
Извините за плохой английский. Спасибо за помощь.