- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一种动态元组结构:
template <typename... Elems> //Should only be tuples
class DynamicTuple {
vector<byte> data; //All data is stored contiguously
vector<tuple<size_t,size_t>> element_table; //First element is offset into the data vector; second is which index of the parameter pack holds the stored type.
/* ... */
}
现在我希望能够过滤掉所有包含类型列表的元组。
template <typename... Ts>
vector<tuple<Ts&...>> filter() {
vector<tuple<Ts&...>> result;
for (auto it : element_table) {
auto [offset, type] = it;
// ???
}
}
这里我需要能够检查“Elems”参数包的第 N 个索引中的类型是否是一个包含“Ts”参数包中所有类型的元组。如果是这样,我想推回一个包含这些值的元组。
凭直觉,我想使用“type”值从“Elems”参数包中获取类型,并使用 has_type 结构,就像这个答案中的结构:https://stackoverflow.com/a/41171291/11463887像这样的东西:
((has_type<Ts, tuple_element<type, tuple<Elems...>>::type>&& ...))
但是这不起作用,因为“类型”不是编译时常量表达式。有办法解决这个问题吗?
最佳答案
随着 has_type
你已经有了,你可以用同样的方式定义一个type_subset
测试元组中是否包含所有类型:
template <typename Ts, typename Tuple>
struct type_subset;
template <typename... Ts, typename... Us>
struct type_subset<std::tuple<Ts...>, std::tuple<Us...>>
: std::conjunction<has_type<Ts, std::tuple<Us...>>...> {};
然后您可能想找到与您的 type
匹配的正确类型通过迭代参数包和相应的索引来索引:
size_t i = 0;
bool match = ((type == i++ && type_subset<std::tuple<Ts...>, Elems>::value) || ...);
确保重置 i
至 0
在每次执行折叠表达式之前。你可能想把整个东西放在一个 lambda 或函数中来分隔 i
并在其他条件下重用它。
也许更好的表现是在编译时将可能的结果保存到一个数组中,然后在运行时对其进行索引:
constexpr static std::array matches{type_subset<std::tuple<Ts...>, Elems>::value...};
bool match = matches[type];
无论哪种情况,您都需要确保 type < sizeof...(Elems)
.特别是在第二个变体中,否则你将有未定义的行为(如果你不使用 .at
而不是 []
)。
关于c++ - 过滤嵌套动态元组(dynamic tuple of tuples),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59352540/
我有一个包含文件名 和文件路径 的元组列表。我想找到重复的 filename(但 filepath 可能不同),即文件名相同但 filepath 可能不同的元组。 元组列表示例: file_info
我有一个像这样定义的变量 auto drum = std::make_tuple ( std::make_tuple ( 0.3f , Ex
我有一个包含几个字段的自定义结构,我想在快速 switch 语句中对其进行模式匹配,这样我就可以通过将其中一个字段与另一个字段进行比较来自定义匹配正则表达式。 例如鉴于这种结构: struct MyS
我有一种动态元组结构: template //Should only be tuples class DynamicTuple { vector data; //All data is st
这个问题在这里已经有了答案: What and When to use Tuple? [duplicate] (5 个答案) 关闭 8 年前。 我正在查看 Tuple 的在线示例,但我没有看到任何理
在我的项目中我有很多坐标要处理,在二维情况下我发现(cons x y)的构造比(list x y)快和 (vector x y)。 但是,我不知道如何将 cons 扩展到 3D 或更进一步,因为我没有
我有以下 Scala 代码: def f(x: Int, y: Int): Option[String] = x*y match { case 0 => None case n =>
我的直觉告诉我,在一般情况下,只有宏或复杂类型的体操才能解决这个问题。 Shapeless 或 Scalaz 可以在这里帮助我吗?这是 N=2 问题的具体实例,但我正在寻找的解决方案适用于所有合理的
为什么这段 Scala 代码是这样的: class Test { def foo: (Int, String) = { (123, "123") } def bar: Unit
我是 python 和 pygame 的新手,我正在尝试学习向量和类的基础知识,但在这个过程中我搞砸了,而且我在理解和修复标题中的错误消息方面苦苦挣扎。 这是我的 Vector 类的代码: impor
我正在编写一个程序来打开和读取一个 txt 文件,并在每一行中循环。将第 2 列和第 4 列中的值相乘并将其分配给第 5 列。 A 500.00 A 84.15 ? B 648.80 B 77.61
我知道还有其他几个问题提出了完全相同的问题,但是当我运行时: 导入命令 从 pyDes 导入 * def encrypt(data, password,): k = des(password,
我有一个元组列表,内容如下: >>>myList [(), (), ('',), ('c', 'e'), ('ca', 'ea'), ('d',), ('do',), ('dog', 'ear', '
给定一个 boost::tuple 和 std::tuple,你如何在它们之间进行转换? 也就是说,您将如何实现以下两个功能? template boost::tuple asBoostTuple(
我无法初始化 std::tuple来自 std::tuple 的逐元素元素兼容类型。为什么它不像 boost::tuple 那样工作? #include #include template st
我是 Storm 的新手并且我正在尝试找出如何编写一个 bolt 测试来测试子类 BaseRichBolt 中的 execute(Tuple tuple) 方法。 问题是 Tuple 似乎是不可变的,
如果我有如下元组列表: [('a', 'b'), ('c', 'd'), ('a', 'b'), ('b', 'a')] 我想删除重复的元组(在内容和内部项目顺序方面重复)以便输出为: [('a',
我编写了一个简单的脚本来模拟基于每用户平均收入 (ARPU)、利润率和客户保持客户的年数 (ltvYears) 的客户生命周期值(value) (LTV)。下面是我的脚本。它在“ltvYears =
以下是我的代码,它是一组元组:。输出:设置([(‘A’,20160129,36.44),(‘A’,20160104,41.06),(‘A’,20160201,37.37)])。如何将另一个元组(‘A’
我用以下代码编写了一个程序: import pandas as pd import numpy as np from typing import Tuple def split_data(self,
我是一名优秀的程序员,十分优秀!