问题
I'd like to pass a vector of pairs to a function. The actual vector implementation as well as the types of the pair should be a template parameter.
I thought of something like this:
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>
The first 3 are other template parameters. The last template parameter should allow to pass a vector
(std
or stxxl:vector
) of std::pair
with either uint32_t
or uint64_t
as type of the pair.first
and pair.second
.
回答1:
You can use this:
template<typename X,
typename Y,
template<typename, typename> class Pair,
template<typename...> class Vector>
void fun(Vector<Pair<X, Y>> vec)
{
//...
}
回答2:
Not sure to understand your requirements but... what about the following example?
#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;
}
回答3:
If I understand you correctly, you want to have a function that takes std::vector
of generic std::pair
. Here you go:
template <typename First, typename Second>
void function(std::vector< std::pair<First,Second> > vector_of_pairs)
{
...
}
EDIT: If you want to take both std::vector
and stxxl::vector
, you can use template template parameters with c++11's variadic templates (since std::vector
and stxxl::vector
have different number of template parameters):
template <typename First,
typename Second,
template <typename...> class AnyVector,
typename... OtherStuff>
void function(AnyVector<std::pair<First,Second>, OtherStuff...> vector_of_pairs)
{
/*...*/
}
来源:https://stackoverflow.com/questions/38396212/vector-of-pairs-with-generic-vector-and-pair-type-template-of-template