c++ - 如何查找元素是否存在于元组中?

标签 c++ tuples c++14

假设我有一个std::tuple:

std::tuple<Types...> myTuple;
// fill myTuple with stuff

现在我想知道 func 是否为 lambda 中的任何元素返回 true,其中 func 是某个 lambda,例如:

auto func = [](auto&& x) -> bool { return someOperation(x); }

我该怎么做?请注意,Types... 可能很大,因此我不想每次都遍历所有 元素。

最佳答案

#include <tuple>

std::tuple<int, char, double> myTuple{ 1, 'a', 3.14f };

bool result = std::apply([](auto&&... args) {
                           return (someOperation(decltype(args)(args)) || ...);
                         }
                       , myTuple);

DEMO

关于c++ - 如何查找元素是否存在于元组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40233067/

相关文章:

c++ - std::tuple_cat 替换失败

c++ - 有人如何定义数据类型,然后在数据类型定义之后立即在同一文件中包含的其他文件中使用它

c++ - 对类中的方法正确使用 `= delete`

android - AppSync Pebble : I get wrong values

python:使用元组的多个变量

c++ - 试图了解C++ 14(N4140)中的[basic.def.odr]/2

c++ - Qt:如何连接 "DialogClass1"的 QPushButton 以终止 "Class2"中的 QProcess?

c++时间戳到人类可读的日期时间函数

c++ - 如何使用整数模板参数实例化模板类的静态成员?

c++ - 从 C++14 切换到 C++11 时修复的对 boost::system::detail::system_category_instance 的 undefined reference