gpt4 book ai didi

c++ - 将多个数组转换为元组

转载 作者:行者123 更新时间:2023-11-28 01:14:13 27 4
gpt4 key购买 nike

是否可以创建一个模板,将可变数量的数组连接成一个元组?或者就此而言,初始化任何聚合或构造一个对象。或者作为函数调用的参数,在具有元组的情况下,可以使用 std::apply 轻松实现。有点像 Python 的 splat 运算符,但对单个对象使用了不止一次。

我知道 std::index_sequence 的各种结构。而且我可以想出一种方法从一个数组中推导出多个索引序列,但仅限于固定数量的数组。例如,对于两个数组:

// Construct tuple from elements of two arrays and index sequences, giving the
// elements the corresponding array to use.
template <typename T1, size_t N1, size_t... IDX1s,
typename T2, size_t N2, size_t... IDX2s>
auto _arrays_to_tuple(const std::array<T1, N1>& a1, std::index_sequence<IDX1s...>,
const std::array<T2, N2>& a2, std::index_sequence<IDX2s...>) {
return std::tuple{ a1[IDX1s]..., a2[IDX2s]... };
}

// Call _arrays_to_tuple() with index sequences for every element in the arrays
template <typename T1, size_t N1,
typename T2, size_t N2>
auto arrays_to_tuple(const std::array<T1, N1>& a1,
const std::array<T2, N2>& a2) {
return _arrays_to_tuple(a1, std::make_index_sequence<N1>{},
a2, std::make_index_sequence<N2>{});
}

void test(void) {
std::array<int, 1> i{1};
std::array<float, 2> f{0.1, 0.2};

std::tuple<int, float, float> x { arrays_to_tuple(i, f) };
extern void foo(int, float, float);
std::apply(foo, arrays_to_tuple(i, f)); // like Python splat: foo(*i, *f)
}

为要构造的类型添加另一个模板参数来代替 std::tuple 很简单。

但是如果不想要固定数量的数组怎么办?代码如下:

std::array<int, 1> i{1};
std::array<float, 2> f{0.1, 0.2};
std::array<short, 3> s{4,5,6};

arrays_to_tuple(i);
arrays_to_tuple(i, f);
arrays_to_tuple(i, f, s);

在一般情况下,是否可以为任意数量的数组创建这样的函数?

最佳答案

STL 有一个很好的函数叫做 std::tuple_cat :

#include <tuple>
#include <array>

template <typename T, size_t N, size_t... IDXs>
auto _array_to_tuple(const std::array<T, N>& a, std::index_sequence<IDXs...>) {
return std::tuple{ a[IDXs]... };
}

template <typename T, size_t N>
auto array_to_tuple(const std::array<T, N>& a) {
return _array_to_tuple(a, std::make_index_sequence<N>{});
}


template<class... Args>
auto arrays_to_tuple(Args&&... args) {
return std::tuple_cat(array_to_tuple(args)...);
}

int main () {
std::array<int, 1> i{1};
std::array<float, 2> f{0.1, 0.2};
std::array<short, 3> s{4,5,6};

std::tuple<int> t1 = arrays_to_tuple(i);
std::tuple<int, float, float> t2 = arrays_to_tuple(i, f);
std::tuple<int, float, float, short, short, short> t3 = arrays_to_tuple(i, f, s);

}

附注:standard告诉我们,那

An implementation may support additional types in the template parameter pack Tuples that support the tuple-like protocol, such as pair and array.

因此,只需将数组直接放入它就可以工作(它在 gcc 9.2.0 和 clang 9.0.0 上对我有用)但是是未定义的行为。

PPS:是的,我作弊了;-P

关于c++ - 将多个数组转换为元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59231104/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com