如何从迭代器中找到一对的类型?

Ler*_*Cpp 0 c++ generic-programming c++11

我在这里以及模板编程方面都是新手。我有一本字典(意味着它可以是std::mapor 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
}
Run Code Online (Sandbox Code Playgroud)

在代码中可以看到,我不知道如何在字典中找到键对的类型/第一和值/第二。谷歌搜索后,我发现我可以通过以下方式找到键值对的类型

using Type = typename std::iterator_traits<Iterator>::value_type;
Run Code Online (Sandbox Code Playgroud)

但不适用于该对个人。有可能找到吗?我正在使用C ++ 11。

对不起,英语不好。谢谢您的帮助。

for*_*818 5

您已经具有maps值类型:

using Type = typename std::iterator_traits<Iterator>::value_type;
Run Code Online (Sandbox Code Playgroud)

maps值类型为std::pair<first_type,second_type>,要获取第一个和第二个类型的对,可以使用其first_typesecond_type

using key_type = typename Type::first_type;
using mapped_type = typename Type::second_type;
Run Code Online (Sandbox Code Playgroud)

要使用key_typeas返回类型,我可能会使用一个小助手:

template <typename Iterator>
struct KeyAndMappedType {
    using value_type = typename std::iterator_traits<Iterator>::value_type;
    using const_key_type = typename value_type::first_type;
    using key_type = typename std::remove_const<const_key_type>::type;
    using mapped_type = typename value_type::second_type;
};
Run Code Online (Sandbox Code Playgroud)

然后

template <typename Iterator, typename B>
typename KeyAndMappedType<Iterator>::key_type AfromB(Iterator begin, Iterator end, B bObj) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

请注意,地图key_type始终是const。由于有时您还需要将该类型也用作非常量,因此我决定KeyAndMapType同时提供这两种类型(也许命名应该颠倒了,即key_typeas const和a non_const_key_type,但是我将由您自己决定细节)。