如何将向量拆分为n个“几乎相等"部分

How to split a vector into n quot;almost equalquot; parts(如何将向量拆分为n个“几乎相等部分)
本文介绍了如何将向量拆分为n个“几乎相等"部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我想使用 ImageMagick 的 convert.exe 合并大量图像,但在 Windows 下我有 8192 字节长的命令行限制.

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.

我对此的解决方案是将任务拆分为较小的子任务,运行它们,然后执行将它们组合在一起的最终任务.

My solution to this is to split the task into smaller sub-task, run them, and do a final task which combines them together.

我的想法是编写一个函数,它接受一个图像向量和一个整数,并将向量拆分为 n 个子向量,所有子向量都具有几乎相等"的部分.

My idea is to write a function, which takes a vector of images and an integer, and splits the vector into n sub-vector all having "almost equal" parts.

例如,如果我想将 11 人分成 3 组,那就是 4-4-3.

So for example if I would like to split 11 into 3 groups it would be 4-4-3.

你能告诉我如何在 C++ 中做到这一点吗?我的意思是,写一个函数

Can you tell me how can I do it in C++? I mean, to write a function

split_vec( const vector<image> &images, int split )

哪个分裂?

另外,你能告诉我如果我不需要创建新向量,只需遍历子部分,最有效的方法是什么吗?像 std::substr 函数和 std::string?

Also, can you tell me what is the most efficient way to do if I don't need to create new vectors, just iterate through the sub-parts? Like the std::substr function with std::string?

注意:我已经在项目中使用了 Boost,所以如果 Boost 中有一些不错的工具,那么它对我来说是完美的.

推荐答案

要得到每个部分大小的基数,只需将总和除以部分数量:11/3 = 3.显然有些部分需要比这更大才能获得正确的总数,但这只是余数:11 % 3 = 2.所以现在您知道其中 2 个部分的尺寸为 3+1,剩下的部分将是 3.

To get a base number for the size of each part, simply divide the total by the number of parts: 11/3 = 3. Obviously some of the parts will need to be bigger than that to get the proper total, but that's just the remainder: 11 % 3 = 2. So now you know that 2 of the parts will be size 3+1, and whatever's left over will be 3.

这篇关于如何将向量拆分为n个“几乎相等"部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)