对传染媒介与普通传染媒介和对类型,模板模板

时间:2016-07-15 12:32:19

标签: c++ templates c++11 vector template-templates

我想将一对矢量传递给一个函数。实际的向量实现以及对的类型应该是模板参数。

我想到了这样的事情:

template<uint8_t t_k,
        typename t_bv,
        typename t_rank,
        template <template <template<typename t_x,
                                     typename t_y> class std::pair>
                  typename t_vector>> typename t_vector>

前3个是其他模板参数。最后一个模板参数应该允许传递vector stdstxxl:vector} std::pair uint32_tuint64_t作为pair.firstpair.second

3 个答案:

答案 0 :(得分:2)

您可以使用:

template<typename X,
         typename Y,
         template<typename, typename> class Pair,
         template<typename...> class Vector>
void fun(Vector<Pair<X, Y>> vec)
{
     //...
}

答案 1 :(得分:1)

如果我理解正确,您希望拥有一个功能,需要std::vector通用std::pair 。你走了:

template <typename First, typename Second>
void function(std::vector< std::pair<First,Second> > vector_of_pairs)
{
  ...
}

编辑:如果你想同时使用std::vectorstxxl::vector,可以使用模板模板参数和c ++ 11的可变参数模板(由于std::vectorstxxl::vector 具有不同数量的模板参数):

template <typename First,
          typename Second,
          template <typename...> class AnyVector,
          typename... OtherStuff>
          void function(AnyVector<std::pair<First,Second>, OtherStuff...> vector_of_pairs)
          {
              /*...*/
          }

答案 2 :(得分:1)

不确定理解您的要求,但......以下示例如何?

#include <iostream>
#include <utility>
#include <vector>
#include <deque>

template <typename P1, typename P2, template<typename...> class Vect>
std::size_t func (const Vect<std::pair<P1, P2>> & v)
 { return v.size(); }

int main()
 {
   std::vector<std::pair<int, long>> v1{ {1, 1L}, {2, 2L}, {3, 3L} };
   std::deque<std::pair<long, int>> v2{ {3L, 1}, {2L, 2} };

   std::cout << func(v1) << std::endl;
   std::cout << func(v2) << std::endl;

   return 0;
 }