2014-11-20 45 views
1

如果我有一個std::array<std::array<int, 4>, 4> mydata,我將如何分割這成小塊或std::array<std::array<int, 2>, 2>,所以它看起來像這樣(視覺):如何將A * A std ::數組拆分爲B * B的塊?

enter image description here

,其中每個顏色是一個std::array<std::array<int, 2>, 2>

我看過一些問題,但他們並不總是問同樣的或不完整:

Split array into chunks in C/C++ - 問如何將數據的一個連續數據流分割成較小的連續位,一維

Split an image into 64x64 chunks - 沒有來源或說明ProcessChunk

+0

需要複製'int'元素。你爲什麼想這樣做?改用視圖是否合理?或者'std:array ,2>'更有意義? – Deduplicator 2014-11-20 20:31:10

+0

引用主陣列是我認爲也適用,只要我可以使用for循環來循環數據 – Gizmo 2014-11-20 20:34:43

+1

4x4陣列都是連續的,但2x2塊不會。這可能會有很大的性能影響。 – Barry 2014-11-20 20:43:30

回答

1

的方式我會做它是由分裂的形象可言。
讓我解釋一下:

如果你創建新的數組,你將不得不陣列對一些新的結構複製,可能的std ::載體,因爲你不知道在編譯時的大小不同。

你可以做什麼,而不是,保持原來的結構,並有一個向量的區域,其中一個區域由4個頂點描述你的內部廣場描述。

struct Area{ 
    std::pair<int,int> topleft; 
    std::pair<int,int> topright; 
    std::pair<int,int> bottomleft; 
    std::pair<int,int> bottomright; 
}; 

vector<Area> areas; 
areas.emplace_back({0,0},{0,2},{2,0},{2,2}); 

如果需要使用更少的信息,你可以只存儲兩個角和計算其他兩個,你其實只需要4個整數知道該地區。

0

寫你自己的功能來做到這一點。這是相當容易的,沿着線的東西:

Array3D splitArray(Array2D inArray, uint rows, uint cols) 
{ 

    //Check if the array size are > 0, skipping this to save time 

    //Check if the arrays 
    size_t inSize1 = inArray.size(); 
    size_t inSize2 = inArray[0].size(); 

    if rows > inSize1 || colus > inSize2 || inSize1 % rows != 0 || inSize2 % cols != 0 
    { 
     //throw an exception 
    } 

    //The arrays split requested is OK, copy the data: 
    //Loop over the inArray and copy the appropriate cells in a temp 2D array 
    //then add that to an array of 2D arrays: 
    std::Array<Array2D> results; 
    return results; 
} 
相關問題