我已經實現了一個對圖像進行計算的類。處理是在給定圖像的一個子集上完成的(可以說每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