gpt4 book ai didi

c++ - 首选哪种数据结构而不是操纵多个 vector

转载 作者:搜寻专家 更新时间:2023-10-31 02:15:07 25 4
gpt4 key购买 nike

我实现了一个对图像进行计算的类。一次对给定图像的一个子集(比如 1000 张中的 100 张)进行处理,每张图像需要不同次数的迭代才能完成。处理使用 GPU,因此不可能一次使用所有图像。图像处理完成后,将删除该图像并添加另一个图像。所以我使用了三个不同的 vector image_outcome , image_index , image_operation保存有关图像的信息:

  1. image_outcomestd::vector<float>它的每个元素都是一个值,用作决定图像何时完成的标准。
  2. image_indexstd::vector<int>保存原始数据集中图像的索引。
  3. image_operationstd::vector<MyEnumValue>包含用于更新 image_outcome 的操作.属于 enum类型及其值是许多可能的操作之一。

还有两个功能,一个是删除完成的图像,一个是添加与删除的图像一样多的图像(如果输入中还有足够的图像)。

  1. remove_images()函数获取所有三个矩阵和图像矩阵,并使用 std::vector.erase() 删除元素.
  2. add_images()再次采用三个矩阵,图像矩阵将新图像和相关信息添加到 vector 中。

因为我使用的是 erase()在具有相同索引(以及类似的添加方式)的每个 vector 上,我正在考虑:

  1. 使用私有(private) struct具有三个 vector (嵌套结构)。
  2. 使用私有(private) class这是使用三个 vector (嵌套类)实现的。
  3. 使用不同于 vec 的数据结构。

代码的高级示例可以在下面找到:

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

最佳答案

我认为,与具有 3 个 vector 的结构相比,用户定义对象的单个 vector 会更好。

std::vector<MyImage> images;

class MyImage {
Image OImage; // the actual image
float fOutcome;
int dIndex;
MyEnumValue eOperation;
bool getIsDone() {
return fOutcome > 0; // random condition
}
}

你可以添加到 vector 或从 vector 中删除一个条件

if( (*it).getIsDone() ) {
VMyVector.erase( it );
}

在我看来,保持 3 个并行的 vector 很容易出错并且很难修改。

关于c++ - 首选哪种数据结构而不是操纵多个 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39079122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com