推入和弹出std::元组的第一个元素
Pushing and popping the first element of a std::tuple
我正在用c++写一个函数,用这种方式有可变数量的参数(和不同类型)
template<typename ...Ts>
void myFunction(Ts ...args)
{
//create std::tuple to access and manipulate single elements of the pack
auto myTuple = std::make_tuple(args...);
//do stuff
return;
}
我想做什么,但我不知道怎么做,是从元组中推送和弹出元素,特别是第一个元素…就像
//remove the first element of the tuple thereby decreasing its size by one
myTuple.pop_front()
//add addThis as the first element of the tuple thereby increasing its size by one
myTuple.push_front(addThis)
这可能吗?
你可以这样做
template <typename T, typename Tuple>
auto push_front(const T& t, const Tuple& tuple)
{
return std::tuple_cat(std::make_tuple(t), tuple);
}
template <typename Tuple, std::size_t ... Is>
auto pop_front_impl(const Tuple& tuple, std::index_sequence<Is...>)
{
return std::make_tuple(std::get<1 + Is>(tuple)...);
}
template <typename Tuple>
auto pop_front(const Tuple& tuple)
{
return pop_front_impl(tuple,
std::make_index_sequence<std::tuple_size<Tuple>::value - 1>());
}
演示请注意,它是非常基本的,不处理引用元组或const限定类型的元组,但它可能足够了。
使用泛型lambda,您可以做得非常优雅:
template<typename Tuple>
constexpr auto pop_front(Tuple tuple) {
static_assert(std::tuple_size<Tuple>::value > 0, "Cannot pop from an empty tuple");
return std::apply(
[](auto, auto... rest) { return std::make_tuple(rest...); },
tuple);
}
std::tuple
的长度和类型在编译时确定。不可能出现运行时弹出或推送。您可以使用std::vector
来提供运行时修改。
您的向量的数据类型可以是std::variant
(c++ 17)或boost::variant
。这两种类型在编译时都接受一个支持类型的列表,并且可以用匹配类型的任何值填充。
或者,您可以使用std::any
(也是c++ 17)或boost::any
来存储任何类型,但具有不同的访问语义。
typedef boost::variant<int, std::string, double> value;
std::vector<value> data;
data.push_back(value(42));
data.psuh_back(value(3.14));
您不能通过添加元素来'延长'元组-这不是元组所代表的。元组的意义在于组成它的不同值之间的绑定连接,如'firstname', 'lastname', 'phone'。
你想要的似乎更容易完成使用一个向量-你可以很容易地添加元素,并删除它们-它的大小可以根据需要任意更改。
相关文章:
- 在boost::hana中给定一个键元组,如何从映射中获取值元组
- 如何使用返回第 n 个元素的方法创建元组
- 从 c++ 中的字符串向量创建一个元组
- 用一个额外的元素扩展 std::array 的每个 std::元组
- 使用对另一个元组不同类型的元素的非常量引用来初始化元组的元素
- 您可以static_assert一个元组只有一种满足特定条件的类型
- 使用参数包和元组创建一个简单的表达式类
- 如何将多个可变参数模板元组类组合成一个类?
- 我如何打印一个元组矢量
- 初始化boost :: hana ::元组用一个参数
- 如何创建一个 std::tuple,其中包含由索引元组指定的向量中的成员
- (C++)生成数组中的第一个p*n完美平方数(从键盘输入p和n)
- c++存储映射元组的引用,以便在另一个函数中使用
- 接受元组并返回另一个元组的功能
- 做一个CPP分类的元组
- 一个奇怪的字符出现在我的字符数组的第一个
- 将参数一个接一个地传递,或通过将它们包裹在数组,结构或元组中
- 对 TC++PL 第 4 版中的元组示例感到困惑
- 创建一个伪元组,一个存储在其他地方的数据的前端
- 元组的一个元素可以引用另一个元素吗