给定std :: tuple,例如:
std::tuple<int, float, char>
我想生成这样的类型:
std::tuple<std::vector<int>, std::vector<float>, std::vector<char>>
你可以看到,它是原始类型向量的元组。
这是标准方案:
typedef std::tuple<int, float, char> Struct; // scenario 1
typedef std::vector<Struct> ArrayOfStructs; // scenario 2
typedef HereIsTheQuestion<Struct>::Type StructOfArrays; // scenario 3
方案1旨在像这样访问:
Struct x = ...; // single tuple
std::get<0>(x) = 11;
// etc.
方案2旨在像这样访问:
ArrayOfStructs xs = ...; // array of tuples
for (size_t i=0; i<xs.size(); ++i) {
std::get<0>(xs[i]) = 11;
// etc.
}
方案3旨在像这样访问:
StructsOfArrays xs = ...; // single tuple of arrays
size_t n = std::get<0>(xs).size(); // first tuple array size
for (size_t i=0; i<n; ++i) {
std::get<0>(xs)[i] = 11;
// etc.
}
如何将HereIsTheQuestion :: Type写成类似于原始Struct类型的数组元组?
谢谢, 米。
答案 0 :(得分:4)
以下是HereIsTheQuestion
的实施方式。
template<typename T> //primary template
struct HereIsTheQuestion; //leave it undefined
template<typename ...T>
struct HereIsTheQuestion<std::tuple<T...>> //partial specialization
{
using Type = std::tuple<std::vector<T>...>;
};
现在
HereIsTheQuestion<std::tuple<int, float, char>>::Type
是
std::tuple<std::vector<int>,std::vector<float>, std::vector<char>>
希望有所帮助。
答案 1 :(得分:3)
您可以使用此模板创建类型:
namespace detail
{
template <typename... Ts>
struct tuple_change { };
template <typename... Ts>
struct tuple_change<std::tuple<Ts...>>
{
using type = std::tuple<std::vector<Ts>...>;
};
}
并创建一个这样的索引序列:
namespace detail
{
template <int... Is>
struct index { };
template <int N, int... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> { };
template <int... Is>
struct gen_seq<0, Is...> : index<Is...> { };
}
您还需要一个模板来允许打印元组:
template <typename... Ts, int... Is>
static void print(std::tuple<Ts...>& var, detail::index<Is...>)
{
auto l = { (print(std::get<Is>(var)), 0)... };
(void)l;
}
template <typename... Ts>
static void print(std::tuple<Ts...>& var)
{
print(var, detail::gen_seq<sizeof...(Ts)>{});
}
template <typename T>
static void print(std::vector<T>& v)
{
for (auto a : v)
{
std::cout << std::boolalpha << a << std::endl;
}
std::cout << std::endl;
}
之后变得简单。这是你的计划:
#include <iostream>
#include <tuple>
#include <vector>
namespace detail
{
template <typename... Ts>
struct tuple_change { };
template <typename... Ts>
struct tuple_change<std::tuple<Ts...>>
{
using type = std::tuple<std::vector<Ts>...>;
};
template <int... Is>
struct index { };
template <int N, int... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> { };
template <int... Is>
struct gen_seq<0, Is...> : index<Is...> { };
}
template <typename... Args, int... Is>
void fill(std::tuple<Args...>& var, detail::index<Is...>)
{
auto l = { (std::get<Is>(var).assign(5, 11), 0)... };
// here I just decided to make the size 5
(void)l;
}
template <typename... Args>
void fill(std::tuple<Args...>& var)
{
fill(var, detail::gen_seq<sizeof...(Args)>{});
}
template <typename T>
static void print(std::vector<T>& v)
{
for (auto a : v)
{
std::cout << std::boolalpha << a << std::endl;
}
std::cout << std::endl;
}
template <typename... Ts, int... Is>
static void print(std::tuple<Ts...>& var, detail::index<Is...>)
{
auto l = { (print(std::get<Is>(var)), 0)... };
(void)l;
}
template <typename... Ts>
static void print(std::tuple<Ts...>& var)
{
print(var, detail::gen_seq<sizeof...(Ts)>{});
}
using result_type = detail::tuple_change<std::tuple<int, bool>>::type;
int main()
{
result_type r;
fill(r);
print(r);
}
的 Demo 强> 的