3

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

我想到了这样的事情:

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::pairwithuint32_tuint64_t作为pair.firstand的类型pair.second

4

3 回答 3

2

你可以使用这个:

template<typename X,
         typename Y,
         template<typename, typename> class Pair,
         template<typename...> class Vector>
void fun(Vector<Pair<X, Y>> vec)
{
     //...
}
于 2016-07-15T12:48:33.367 回答
1

如果我对您的理解正确,您希望拥有一个具有generic的功能std::vectorstd::pair。干得好:

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

编辑:如果你想同时使用std::vectorand stxxl::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)
          {
              /*...*/
          }
于 2016-07-15T12:55:53.047 回答
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;
 }
于 2016-07-15T13:00:13.323 回答