如何使用range-v3压缩向量的向量 [英] How to zip vector of vector with range-v3

查看:223
本文介绍了如何使用range-v3压缩向量的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这是 sum-vector with range-v3 的后续版本)

如果我有两个(或更多)向量,可以将ziprange-v3一起使用,如下所示:

If I have two (or more) vectors, I can zip them together with range-v3 like this:

std::vector< int > v1{1,1,1};
std::vector< int > v2{2,2,2};

auto v = ranges::views::zip( v1, v2 )
  | ranges::views::transform( ... );

这很好用,但是在实践中,我没有显式矢量,但是我有矢量的矢量.我想执行以下操作,但不会得到相同的结果. (实际上,我不确定结果是什么,也不知道如何确定结果是!)

This works well, but in practice, I don't have explicit vectors, but I do have a vector of vectors. I'd like to do the following, but it doesn't give the same result. (In fact, I'm not sure what the result is, and I don't know how to determine what the result is!)


std::vector< std::vector< int > > V{{1,1,1},{2,2,2}};

auto vV = ranges::views::zip( V )
  | ranges::views::transform( ... );

如何像压缩一些显式矢量一样压缩vector< vector >?我尝试将joinstridechunk等一起使用.但还没有找到魔术组合.

What can I do to zip a vector< vector > like I did to zip a few explicit vectors? I've tried using join along with stride, chunk, etc. but haven't found the magic combination.

推荐答案

我想,如果您在编译时不知道外部vector的大小,那么唯一可行的解​​决方案就是解决它.我建议不要使用zip,而不要尝试使用zip,因为在这种情况下它似乎更加通用.

I suppose if you don't know the size of external vector in compile time the only reasonable solution that remains is work around it. Instead of trying to zip it I would suggest going with accumulate on top of that as it seems more versatile in this situation.

std::vector< std::vector< int > > V{{1,1,1},{2,2,2}};

auto vV = ranges::accumulate(
    V,
    std::vector<ResultType>(V[0].size()),
    [](const auto& acc, const auto& next) {
        auto range = ranges::views::zip(acc, next) | ranges::views::transform(...);
        return std::vector<int>(range.begin(), range.end());
    }
)

我忘了必须复制范围.

I forgot the range has to be copied.

这篇关于如何使用range-v3压缩向量的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆