简体   繁体   中英

Split vector by size

I've a buffer, which contains a JPEG-Image.

Now I want to split this vector in parts of max. 64000Bytes.

If I have an Array-Size of 100000: 1. Array = 64000 2. Array = 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.

With that disclosure made, if your input is buff you can do this:

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.

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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