C++:使用 std::apply 分两部分遍历元组:直到应用 lambda 的索引,然后在应用不同 lambda 的索引之后
C++: Using std::apply to iterate through a tuple in 2 parts: up to an index applying a lambda and then after an index applying a different lambda
如果我想使用 std::apply
遍历一个元组但不对整个元组应用一个函数,我该如何分离元组,即将一个函数应用于第一个 n
值和另一个到它之后的所有值?
some_values
将是一个可以具有任何长度和类型的元组,并且 length_of_first_part
(可能命名不那么冗长)在编译时已知。
std::tuple<char, long long, double, long double, float> some_values(33, 2, 3.4, 5.6, 7.8);
const size_t length_of_first_part = 2;
std::apply(
[](auto&&... current_val) {
((std::cout << "(Should be first part) " << current_val << "\n"), ...); //Would obviously do a litle more than cout, but this is just a minimal example
}, some_values
);
std::apply(
[](auto&&... current_val) {
((std::cout << "(Should be second part) " << current_val << "\n"), ...);
}, some_values
);
试一试。在这里查看“可能的实现”:
https://en.cppreference.com/w/cpp/utility/apply
它将 std::index_sequence
传递给辅助函数以调用 std::invoke
。我将其更改为传递 std::make_sequence<N>
(N 是拆分索引)而不是元组大小。然后我通过倒数(元组大小 - N)在第二个函数上调用 std::invoke
:
template <int N, typename F1, typename F2, typename Tuple, size_t... I1s, size_t... I2s>
decltype(auto) split_apply_impl(F1&& f1, F2&& f2, Tuple&& t, std::index_sequence<I1s...>, std::index_sequence<I2s...>) {
std::invoke(std::forward<F1>(f1), std::get<I1s>(std::forward<Tuple>(t))...);
return std::invoke(std::forward<F2>(f2), std::get<(I2s+N)>(std::forward<Tuple>(t))...);
}
template <size_t N, typename F1, typename F2, typename Tuple>
decltype(auto) split_apply(F1&& f1, F2&& f2, Tuple&& t)
{
return split_apply_impl<N>(
std::forward<F1>(f1),
std::forward<F2>(f2),
std::forward<Tuple>(t),
std::make_index_sequence<N>{},
std::make_index_sequence<(std::tuple_size_v<std::remove_reference_t<Tuple>>-N)>{});
}
用法:
std::tuple<char, long long, double, long double, float> some_values(33, 2, 3.4, 5.6, 7.8);
constexpr size_t length_of_first_part = 2;
split_apply<length_of_first_part>(
[](auto&&... current_val) {
((std::cout << "(Should be first part) " << current_val << "\n"), ...);
},
[](auto&&... current_val) {
((std::cout << "(Should be second part) " << current_val << "\n"), ...);
},
some_values);
如果我想使用 std::apply
遍历一个元组但不对整个元组应用一个函数,我该如何分离元组,即将一个函数应用于第一个 n
值和另一个到它之后的所有值?
some_values
将是一个可以具有任何长度和类型的元组,并且 length_of_first_part
(可能命名不那么冗长)在编译时已知。
std::tuple<char, long long, double, long double, float> some_values(33, 2, 3.4, 5.6, 7.8);
const size_t length_of_first_part = 2;
std::apply(
[](auto&&... current_val) {
((std::cout << "(Should be first part) " << current_val << "\n"), ...); //Would obviously do a litle more than cout, but this is just a minimal example
}, some_values
);
std::apply(
[](auto&&... current_val) {
((std::cout << "(Should be second part) " << current_val << "\n"), ...);
}, some_values
);
试一试。在这里查看“可能的实现”: https://en.cppreference.com/w/cpp/utility/apply
它将 std::index_sequence
传递给辅助函数以调用 std::invoke
。我将其更改为传递 std::make_sequence<N>
(N 是拆分索引)而不是元组大小。然后我通过倒数(元组大小 - N)在第二个函数上调用 std::invoke
:
template <int N, typename F1, typename F2, typename Tuple, size_t... I1s, size_t... I2s>
decltype(auto) split_apply_impl(F1&& f1, F2&& f2, Tuple&& t, std::index_sequence<I1s...>, std::index_sequence<I2s...>) {
std::invoke(std::forward<F1>(f1), std::get<I1s>(std::forward<Tuple>(t))...);
return std::invoke(std::forward<F2>(f2), std::get<(I2s+N)>(std::forward<Tuple>(t))...);
}
template <size_t N, typename F1, typename F2, typename Tuple>
decltype(auto) split_apply(F1&& f1, F2&& f2, Tuple&& t)
{
return split_apply_impl<N>(
std::forward<F1>(f1),
std::forward<F2>(f2),
std::forward<Tuple>(t),
std::make_index_sequence<N>{},
std::make_index_sequence<(std::tuple_size_v<std::remove_reference_t<Tuple>>-N)>{});
}
用法:
std::tuple<char, long long, double, long double, float> some_values(33, 2, 3.4, 5.6, 7.8);
constexpr size_t length_of_first_part = 2;
split_apply<length_of_first_part>(
[](auto&&... current_val) {
((std::cout << "(Should be first part) " << current_val << "\n"), ...);
},
[](auto&&... current_val) {
((std::cout << "(Should be second part) " << current_val << "\n"), ...);
},
some_values);