如何将元素中的数据分组为元组向量

发布于 2025-01-25 06:51:28 字数 1863 浏览 4 评论 0 原文

我想将元素中的元素数据分组为室内的元组。 给定的是包含数据的元组元组。有多种重复类型,该数据应分组为每种唯一类型的向量。

到目前为止 boost :: mp11 是我发现的最优雅的方式 std :: tuple< std :: vector< t1>,std :: tuple< std :: vector< t2>,...> 基于传入参数包 std :: tuple< std :: tuple< some_data>,std :: tuple&lt< std :: tuple< some_more_more_data>,... 使用 mp_unique mp_transform 获取每种唯一类型的向量。

如果您知道 std :: ish替代方案(可选),请开心。

我被困在寻找一种将元组元素填充到匹配矢量中的方法吗? 我很兴奋,找到一种折叠的表达方式。

这个示例应该有助于更好地了解我的想法。

template <typename T> using vector_of = std::vector<T>;

static constexpr auto tuple_to_vector(auto&&... Vs) noexcept {
    // merged_tuple = std::tuple<int, double, int, char, int, float, char, float, double>
    auto merged_tuple = std::tuple_cat(std::forward<decltype(Vs)>(Vs)...);
  
    // vector_t = std::tuple<std::vector<int>, std::vector<double>, std::vector<char>, std::vector<float>>
    using vector_t = boost::mp11::mp_transform<vector_of, boost::mp11::mp_unique<decltype(merged_tuple)>>; 
    vector_t vec;

    // how to add merged_tuple elements to vec
    // resulting in
    // std::tuple< std::vector<int>{1,3,2}, std::vector<double>{2.0,3.0}, std::vector<char>{'b','c'}, std::vector<float>{3.0f,2.0f}>
  
    return std::move(vec);
    
};

int main() {
    constexpr auto vec = tuple_to_vector(
        std::make_tuple(1,2.0,3),
        std::make_tuple('b',2,3.0f),
        std::make_tuple('c',2.0f,3.0)
    );
    // expecting
    // vec = std::tuple<
    //  std::vector<int>{1,3,2},
    //  std::vector<double>{2.0,3.0},
    //  std::vector<char>{'b','c'},
    //  std::vector<float>{3.0f,2.0f}
    // >
    return 42;
}

I want to group data of tuples in tuples into a tuple of vectors.
Given is a tuple of tuples containing data. There are multiple duplicate types, that's data should be grouped into a vector of each unique type.

So far boost::mp11 is the most elegant way I found, to build a type
std::tuple<std::vector<T1>, std::tuple<std::vector<T2>, ...>
based on an incoming parameter pack
std::tuple<std::tuple<some_data>, std::tuple<std::tuple<some_more_data>, ...
using mp_unique and mp_transform to get vectors for each unique type.

Happy if you know a std::ish alternative (optional).

I am stuck finding a way to fill tuple elements into matching vectors?
I'd be excited, finding a fold expression'ish way to do so.

This example should help to give a better idea of what I have in mind.

template <typename T> using vector_of = std::vector<T>;

static constexpr auto tuple_to_vector(auto&&... Vs) noexcept {
    // merged_tuple = std::tuple<int, double, int, char, int, float, char, float, double>
    auto merged_tuple = std::tuple_cat(std::forward<decltype(Vs)>(Vs)...);
  
    // vector_t = std::tuple<std::vector<int>, std::vector<double>, std::vector<char>, std::vector<float>>
    using vector_t = boost::mp11::mp_transform<vector_of, boost::mp11::mp_unique<decltype(merged_tuple)>>; 
    vector_t vec;

    // how to add merged_tuple elements to vec
    // resulting in
    // std::tuple< std::vector<int>{1,3,2}, std::vector<double>{2.0,3.0}, std::vector<char>{'b','c'}, std::vector<float>{3.0f,2.0f}>
  
    return std::move(vec);
    
};

int main() {
    constexpr auto vec = tuple_to_vector(
        std::make_tuple(1,2.0,3),
        std::make_tuple('b',2,3.0f),
        std::make_tuple('c',2.0f,3.0)
    );
    // expecting
    // vec = std::tuple<
    //  std::vector<int>{1,3,2},
    //  std::vector<double>{2.0,3.0},
    //  std::vector<char>{'b','c'},
    //  std::vector<float>{3.0f,2.0f}
    // >
    return 42;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠书 2025-02-01 06:51:28

您可以使用 std :: apply MERGED_TUPLE 的元素,并使用 std :: get 根据该类型提取相应的 vector vector_t 中根据该类型该元素的内容,然后通过 vector 通过 push_back

std::apply([&vec](auto... args) {
   (std::get<std::vector<decltype(args)>>(vec).push_back(args), ...);
}, merged_tuple);

demo

请注意, vec 不能 constexpr 即使在C ++ 20中,由于其分配是非传播的。如果您真的想构造 constexpr 元组,则可以使用 std ::数组,因为可以在编译时获得数组的大小。这是 demo 将结果转换为相应的 vec std ::数组类型。

You can use std::apply to expand the elements of merged_tuple, and use std::get to extract the corresponding vector in vector_t according to the type of the element, and fill into the vector through push_back

std::apply([&vec](auto... args) {
   (std::get<std::vector<decltype(args)>>(vec).push_back(args), ...);
}, merged_tuple);

Demo

Note that vec cannot be constexpr even in C++20 since its allocation is non-transient. If you really want to construct a constexpr tuple, then you can use std::array since the size of the array can be obtained at compile time. Here is a demo that converts the resulting vec to the corresponding std::array type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文