我已经实现了一个对图像进行计算的类。处理是在给定图像的一个子集上完成的(可以说每1000个中有100个),并且每个图像都需要不同次数的迭代才能完成。处理使用GPU,因此不可能一次使用所有图像。当图像的处理完成后,该图像被移除并且另一个被添加。所以我用三个不同的载体image_outcome
,image_index
,image_operation
保持infromations有关图片:更喜欢什么数据结构而不是操纵多个向量
- 的
image_outcome
是std::vector<float>
和它的每个元素是作为标准来决定何时该图像是一个值完了。 image_index
是一个std::vector<int>
,它保存原始数据集中图像的索引。image_operation
是std::vector<MyEnumValue>
,其中包含用于更新image_outcome
的操作。属于enum
类型,其值是许多可能的操作之一。
还有两个功能,一个用于删除已完成的图像,另一个用于添加尽可能多的图像(如果输入中仍有足够的图像)。
remove_images()
函数获取所有三个矩阵和图像矩阵,并使用std::vector.erase()
删除元素。add_images()
再次采用三个矩阵,图像矩阵将新图像和相关信息添加到矢量。
因为我使用的具有相同索引每个向量的erase()
(也以类似的方式来添加)我当时就想:
- 使用私人
struct
有三个矢量(嵌套结构)。 - 使用使用三个向量(嵌套类)实现的私有
class
。 - 使用vec以外的其他数据结构。
代码的一块进行水平例如可以是以下基金:代替用3个载体,用户定义的对象的单个载体将更好地工作一个struct
class ComputationClass {
public:
// the constructor initializes the member variables
ComputationClass();
void computation_algorithm(std::vector<cv::Mat> images);
private:
// member variables which define the algorithms parameters
// add_images() and remove_images() functions take more than these
// arguments, but I only show the relevant here
add_images(std::vector<float>&, std::vector<int>&, std::vector<MyEnumValue>&);
remove_images(std::vector<float>&, std::vector<int>&, std::vector<MyEnumValue>&);
};
void ComputationClass::computation_algorithm(std::vector<cv::Mat> images) {
std::vector<float> image_output;
std::vector<int> image_index;
std::vector<MyEnumValue> image_operation;
add_images(image_output, image_index, image_operation);
while (there_are_still_images_to_process) {
// make computations by updating the image_output vector
// check which images finished computing
remove_images(image_output, image_index, image_operation);
add_images(image_output, image_index, image_operation);
}
}
包含“float”,“int”和“MyEnumValue”的结构,然后是该结构的向量? –
关联在一起的数据应该保持在一起,否则最终难以维护它。它也会减少缓存失误。 – StoryTeller