- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以创建一个模板,将可变数量的数组连接成一个元组?或者就此而言,初始化任何聚合或构造一个对象。或者作为函数调用的参数,在具有元组的情况下,可以使用 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/
typing模块中使用List、Tuple等有什么区别: from typing import Tuple def f(points: Tuple): return map(do_stuff,
如何遍历列表的每 5 个元素并将它们组成一个元组,然后将同一列表的第 6 个元素作为第二个元组 - 然后对接下来的 5 个元素和第 6 个元素执行相同的操作。 我读过 operator.itemget
我有一个 Seq[((元组 A),(元组 B))] 有没有一种简单的方法来对元组 A 进行分组,以便我得到 Seq[(Tuple A, Seq[Tuple B])] 我试过 groupby(x =>
如果我有以下内容 val A = List(1,2,3) val B = List(1,2,3) 这两个变量是否有相同的内存地址? 最佳答案 它们不会有相同的内存地址,可以使用 eq 方法确认,com
我实际上是在尝试创建一个配对列表,但事实证明这非常困难 在有人提到 Hashtables 之前请注意,会有我不关心的重复项。 例如,如果我这样做 $b = @{"dog" = "cat"} 我明白了
我正在尝试为其他资源中的 for_each 循环创建局部变量,但无法按预期制作局部映射。 以下是我试过的。 (地形 0.12) 预期映射到循环 temple_list = { "test2-role"
我目前正在学习 Haskell,在 FP 方面我绝对是初学者。 现在我正在尝试使用列表推导式进行不同的操作。 listComprehension = [(a,b,c) | a <- xs, b <
我正在尝试为其他资源中的 for_each 循环创建局部变量,但无法按预期制作局部映射。 以下是我试过的。 (地形 0.12) 预期映射到循环 temple_list = { "test2-role"
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
如何通过元组中的第三项过滤此类型的列表: type Car = (String, [String], Int [String]) 我看到了 snd和 fst方法,但在这里我认为这行不通,我不确定如何在
有没有办法创建 Tuple 在 Java 中,无需创建多个类? 例如,可以为每种不同类型的元组创建不同的类,每个类具有不同数量的 Type Parameters : public class Sing
我必须处理一堆二维点类型:pair , pair , pair ,并且只要存在坐标转换,我就允许点之间的隐式转换。像这样: template inline operator pair ( pair t
这个问题在这里已经有了答案: How do I iterate through two lists in parallel? (8 个答案) How do I iterate over the tu
编写一个函数 square_odd_terms 接受一个元组作为参数并返回一个元组中奇数项的平方的元组。即使是条款也将保持不变。 我的尝试是: def square_odd_termms(tpl):
更新: 我选择了这个: set(item[1] for item in id) 谢谢你们,你们的想法对我有帮助。 我正在处理一个元组列表: 以下面这行代码为例。我的 list 可以是任何长度。但是,我
我一直在尝试执行此任务,在尝试时我不禁想到会有比我一直尝试的方式更好的编码方式。 我有一行文字和一个关键字。我想在每个列表中的每个字符下创建一个新列表。关键字将重复自身直到列表末尾。如果有任何非字母字
我现在这个问题已经被问过好几次了。但是,答案似乎并没有解决我的问题。我收到类型错误,“元组”对象不可调用。即使列表中的元组以正确的方式用逗号分隔,我也得到了这个: def aiMove(b):
嘿,所以我花了两个多小时试图解决这个问题,但我就是做不对。我猜我犯了一个非常简单的错误,所以如果有人能指出我正确的方向,我将非常感激,谢谢!顺便说一句,这是一门树屋类(class)。 “目前我们的问候
这不是一个严格的编程问题,但为什么是tuple在单独的 header 中定义,而不是添加到 连同 pair ?它看起来更自然,不那么困惑等。 最佳答案 在具有细粒度的 header 和只有一个 hea
我是一名优秀的程序员,十分优秀!