如何从标准::元组中删除元素?

How to drop element from std::tuple?

本文关键字:删除 元素 元组 标准      更新时间:2023-10-16

给定一个

struct A{}; struct B{}; struct C{};
std::tuple<A,B,C> tabc;

如何从中删除第二个元素B以获得tuple<A,C>,例如

std::tuple<A,C> tac = drop<B>(tabc);

或相同

std::tuple<A,C> tac = drop<1>(tabc);

我假设这会产生一个带有元素副本的新类型。

这相当简单,可以用元编程技术进行编码:

template<size_t drop, size_t ...ixs>
constexpr auto calc_drop_sequence_dropper(std::index_sequence<ixs...>)
{
return std::index_sequence<(ixs >= drop ? ixs + 1 : ixs)...>{};
}
//Creates a monotonically increasing sequence on the range [0, `count`), except
//that `drop` will not appear.
template<size_t count, size_t drop>
constexpr auto calc_drop_copy_sequence()
{
static_assert(count > 0, "You cannot pass an empty sequence.");
static_assert(drop < count, "The drop index must be less than the count.");
constexpr auto count_sequence = std::make_index_sequence<count - 1>();
return calc_drop_sequence_dropper<drop>(count_sequence);
}
template<typename Tuple, size_t ...ixs>
constexpr auto copy_move_tuple_by_sequence(Tuple &&tpl, std::index_sequence<ixs...>)
{
using TplType = std::remove_reference_t<Tuple>;
return std::tuple<std::tuple_element_t<ixs, TplType>...>(
std::get<ixs>(std::forward<Tuple>(tpl))...);
}
template<size_t drop, typename Tuple>
constexpr auto drop_tuple_element(Tuple &&tpl)
{
using TplType = std::remove_reference_t<Tuple>;
constexpr size_t tpl_size = std::tuple_size<TplType>::value;
constexpr auto copy_seq = calc_drop_copy_sequence<tpl_size, drop>();
return copy_move_tuple_by_sequence(std::forward<Tuple>(tpl), copy_seq);
}

主函数是drop_tuple_element,它执行假设的drop函数所做的事情。当然,如果您要删除多个元素,则希望一次删除所有元素,而不是单独删除。因此,您需要修改代码。

看看这个答案如何制作一个元组:

创建从 std::tuple

阅读它。通过制作一个元组例程,你可以做一个通用版本的drop

template<size_t index, typename Tuple>
auto Drop(const Tuple& t)
{
return std::tuple_cat(
subtuple_v2<0,index>(t), 
subtuple_v2<index+1, std::tuple_size<Tuple>::value>(t));
}

其中subtuple_v2<a,b>是帖子的扩展版本,它从索引a生成子元组b

std::tuple 是不可变的,你不能删除或删除。

std::tuple 是异构值的固定大小集合

根据您的C++口味,创建一个具有 std::get(std::tuple( 和 std::make_tuple 的新元组

auto tac = std::make_tuple(std::get<0>(tabc), std::get<2>(tabc));

或与 C++14

auto tac = std::make_tuple(std::get<A>(tabc), std::get<C>(tabc));

如果您知道元组中对象 A 和 C 的索引,则可以将函数std::get与模板参数size_t一起使用。否则,可以将函数std::get与类型模板参数一起使用。

这是一个演示程序

#include <iostream>
#include <tuple>
struct A
{
};
struct B
{
};
struct C
{
};
int main() 
{
auto t1 = std::make_tuple( A(), B(), C() );
auto t2 = std::make_tuple( std::get<A>( t1 ), std::get<C>( t1 ) );
auto t3 = std::make_tuple( std::get<0>( t1 ), std::get<2>( t1 ) );
return 0;
}