How to split a vector into n “almost equal” parts

后端 未结 7 662
野性不改
野性不改 2020-12-18 20:03

I have a problem that I would like to merge a large number of images using ImageMagick\'s convert.exe, but under Windows I have a 8192 byte long command line limit.

7条回答
  •  猫巷女王i
    2020-12-18 20:35

    You don't have to create new sub-vectors, use something like following:

    size_t ProcessSubVec(const vector& images, size_t begin, size_t end)
    {
        // your processing logic
    }
    
    void SplitVec(const vector& images, int cnt)
    {
        size_t SubVecLen = images.size() / cnt,
               LeftOvers = images.size() % cnt,
               i = 0;
    
        // Split into "cnt" partitions
        while(i < images.size())
            i += ProcessSubVec(images, i, i + SubVecLen + (LeftOvers-- == 0 ? 0 : 1));
    }
    

    Hope this helps.

提交回复
热议问题