在 C++ 中对除最后一个元组之外的所有元素进行算术运算
Arithmetic on all but the last components of tuples in C++
从 中获得灵感,我设法实现了一个函数,它将两个元组作为输入,return 一个元组,其组件是输入中给定的元组的每个组件的最小值。
template<typename T, T...>
struct integer_sequence { };
template<std::size_t N, std::size_t... I>
struct gen_indices : gen_indices<(N - 1), (N - 1), I...> { };
template<std::size_t... I>
struct gen_indices<0, I...> : integer_sequence<std::size_t, I...> { };
template <typename ... T, std::size_t ... I>
std::tuple<T...> min_tuple_impl(
const std::tuple<T...>& t1,
const std::tuple<T...>& t2,
integer_sequence<std::size_t, I...>)
{
return { (std::min(std::get<I>(t1), std::get<I>(t2)))... };
}
template <typename ... T>
std::tuple<T...> min_tuple(
const std::tuple<T...>& t1,
const std::tuple<T...>& t2)
{
return min_tuple_impl(t1, t2, gen_indices<sizeof...(T)>{});
}
现在,我希望它适用于除最后一个组件之外的所有组件。
std::tuple<int, int, int> t1 {10, 20, 30}
std::tuple<int, int, int> t2 {15, 15, 15}
min_tuple(t1, t2) // == {10, 15, 30}, for the last one, always keep the component of t1
我如何在 C++11 中得到这个?
Now, I would like it to work on all components but the last one.
for the last one, always keep the component of t1
因此,要解决此问题,请传递一个简化的整数列表(不是 sizeof...(T)
,而是 sizeof...(T)-1u
):
template <typename ... T>
std::tuple<T...> min_tuple(
const std::tuple<T...>& t1,
const std::tuple<T...>& t2)
{
return min_tuple_impl(t1, t2, gen_indices<sizeof...(T)-1u>{});
} // .....................................................^^^
最后,添加最后一个 t1
元素:
template <typename ... T, std::size_t ... I>
std::tuple<T...> min_tuple_impl(
const std::tuple<T...>& t1,
const std::tuple<T...>& t2,
integer_sequence<std::size_t, I...>)
{
return { (std::min(std::get<I>(t1), std::get<I>(t2)))...,
std::get<sizeof...(I)>(t1) };
} // .........^^^^^^^^^^^^^^^^^^^^^^^^^^
注意:您必须确保 sizeof...(T)
严格大于零。
从
template<typename T, T...>
struct integer_sequence { };
template<std::size_t N, std::size_t... I>
struct gen_indices : gen_indices<(N - 1), (N - 1), I...> { };
template<std::size_t... I>
struct gen_indices<0, I...> : integer_sequence<std::size_t, I...> { };
template <typename ... T, std::size_t ... I>
std::tuple<T...> min_tuple_impl(
const std::tuple<T...>& t1,
const std::tuple<T...>& t2,
integer_sequence<std::size_t, I...>)
{
return { (std::min(std::get<I>(t1), std::get<I>(t2)))... };
}
template <typename ... T>
std::tuple<T...> min_tuple(
const std::tuple<T...>& t1,
const std::tuple<T...>& t2)
{
return min_tuple_impl(t1, t2, gen_indices<sizeof...(T)>{});
}
现在,我希望它适用于除最后一个组件之外的所有组件。
std::tuple<int, int, int> t1 {10, 20, 30}
std::tuple<int, int, int> t2 {15, 15, 15}
min_tuple(t1, t2) // == {10, 15, 30}, for the last one, always keep the component of t1
我如何在 C++11 中得到这个?
Now, I would like it to work on all components but the last one.
for the last one, always keep the component of t1
因此,要解决此问题,请传递一个简化的整数列表(不是 sizeof...(T)
,而是 sizeof...(T)-1u
):
template <typename ... T>
std::tuple<T...> min_tuple(
const std::tuple<T...>& t1,
const std::tuple<T...>& t2)
{
return min_tuple_impl(t1, t2, gen_indices<sizeof...(T)-1u>{});
} // .....................................................^^^
最后,添加最后一个 t1
元素:
template <typename ... T, std::size_t ... I>
std::tuple<T...> min_tuple_impl(
const std::tuple<T...>& t1,
const std::tuple<T...>& t2,
integer_sequence<std::size_t, I...>)
{
return { (std::min(std::get<I>(t1), std::get<I>(t2)))...,
std::get<sizeof...(I)>(t1) };
} // .........^^^^^^^^^^^^^^^^^^^^^^^^^^
注意:您必须确保 sizeof...(T)
严格大于零。