简体   繁体   English

按大小拆分矢量

[英]Split vector by size

I've a buffer, which contains a JPEG-Image. 我有一个缓冲区,其中包含一个JPEG图像。

Now I want to split this vector in parts of max. 现在我想将这个向量分成最大部分。 64000Bytes. 64000Bytes。

If I have an Array-Size of 100000: 1. Array = 64000 2. Array = 36000 如果我的阵列大小为100000:1。数组= 64000 2.数组= 36000

How should I do this? 我该怎么做?

This is my code: But I didnt know how to split the array. 这是我的代码:但我不知道如何拆分数组。

std::vector<uchar> buff;
for(int i = 0; i < buff.size(); i++)
{
        if(i % 64000 == 0 && i != 0)
        {
            std::cout << "Package Size" << i << std::endl;

        }
}

It's easy to split an array but if you're hoping that each portion of the vector will contain a valid .jpeg, I believe that you will be sorely disappointed. 分割数组很容易,但是如果你希望向量的每个部分都包含一个有效的.jpeg,我相信你会非常失望。

With that disclosure made, if your input is buff you can do this: 有了这个披露,如果你的输入是buff你可以这样做:

const auto size = 64000;
std::vector<std::vector<uchar>> foo(buff.size() / size, std::vector<uchar>(size));
foo.push_back(std::vector<uchar>(buff.size() % size);

for(auto i = 0; i < buff.size(); ++i)foo[i / size][i % size] = buff[i];

It's very easy once you know that the std::vector constructor` can take a range from two iterators. 一旦你知道std::vector构造函数std::vector可以从两个迭代器获取范围,这很容易。

So you can do eg 所以你可以这样做

std::vector<uchar> buff;

...

std::vector<uchar> v1(buff.begin(), buff.begin() + 64000);
std::vector<uchar> v2(buff.begin() + 64000, buff.end());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM