C++11-元组数组中的元组
C++ 11 - Tuple of Arrays from Array of Tuple
给定一个std::元组,例如:
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类型的数组元组?
谢谢,m.
以下是应该如何实现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>>
希望能有所帮助。
您可以使用此模板创建类型:
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);
}
演示
相关文章:
- COM 接口 c# 封送数组数组
- 使用数组数组调用 C++ 函数
- 为什么我们必须在 C++ 中释放数组数组,而不仅仅是整个"matrix"
- 在C++中返回基元类型数组(Java比较)
- 形成元组数组C++
- 如何在 C++ 中定义数组数组
- 如何正确分配动态创建int并将其分配给具有值的动态数组数组
- 将指针返回使用New创建的数组数组
- 一种从元组/数组获取参数包的方法
- 如何在 HDF5 中逐个存储组数组
- 如何在 c++ 中将字符串拆分为字符串 2d 数组数组
- 如何将字符串数组转换为char数组数组
- 当将多个TypedArrayContents数组加载到V8数组中时(数组数组)时,最后一个元素将覆盖所有元素
- C AVX2:访问数组数组中的地址时SEG故障
- 不能和正确的方法来声明类成员 c++11 中的 int 数组数组
- 包含字符数组数组的结构
- 根据函数名称数组数组的索引调用函数
- 不同的计数器,同一数组.数组元素不变.C
- C++11-元组数组中的元组
- 从QSettings中读取自定义元类型数组