2014-11-20 46 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; 
} 
相关问题