Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 按特定顺序迭代数组元素_C++_Arrays_Vector_Data Structures - Fatal编程技术网

C++ 按特定顺序迭代数组元素

C++ 按特定顺序迭代数组元素,c++,arrays,vector,data-structures,C++,Arrays,Vector,Data Structures,我试图以特定的顺序迭代一个一维整数数组/向量,但我不能绕着它转,以获得正确的循环条件 输入数据是整数的一维向量: {1, 5, 9, 13, 17, 21, 2, 6, 10, 14, 18, 22, 3, 7, 11, 15, 19, 23, 4, 8, 12, 16, 20, 24, 25, 29, 33, 37, 41, 45, 26, 30, 34, 38, 42, 46, 27, 31, 35, 39, 43, 47, 28, 32, 36, 40, 44, 48} 该输入数据本质上

我试图以特定的顺序迭代一个一维整数数组/向量,但我不能绕着它转,以获得正确的循环条件

输入数据是整数的一维向量:

{1, 5, 9, 13, 17, 21, 2, 6, 10, 14, 18, 22, 3, 7, 11, 15, 19, 23, 4, 8, 12, 16, 20, 24, 25, 29, 33, 37, 41, 45, 26, 30, 34, 38, 42, 46, 27, 31, 35, 39, 43, 47, 28, 32, 36, 40, 44, 48}
该输入数据本质上是二维阵列/栅格的一维表示,包含子组:

+----+----+----+----+----+----+
| 1  | 5  | 9  | 13 | 17 | 21 |
+----+----+----+----+----+----+
| 2  | 6  | 10 | 14 | 18 | 22 |
+----+----+----+----+----+----+
| 3  | 7  | 11 | 15 | 19 | 23 |
+----+----+----+----+----+----+
| 4  | 8  | 12 | 16 | 20 | 24 |
+----+----+----+----+----+----+
| 25 | 29 | 33 | 37 | 41 | 45 |
+----+----+----+----+----+----+
| 26 | 30 | 34 | 38 | 42 | 46 |
+----+----+----+----+----+----+
| 27 | 31 | 35 | 39 | 43 | 47 |
+----+----+----+----+----+----+
| 28 | 32 | 36 | 40 | 44 | 48 |
+----+----+----+----+----+----+
int grid_width = 6; // Variable, but will always be a multiple of 2, to match subgroup width
int grid_height = 8;  // Variable, but will always be a multiple of 4, to match subgroup height
const int group_width = 2; // Constant, subgroup width is always 2.
const int group_height = 4; // Constant, subgroup height is always 4.
此表分为多个2x4子组:

+----+----+----+----+----+----+
| 1  | 5  | 9  | 13 | 17 | 21 |
+----+----+----+----+----+----+
| 2  | 6  | 10 | 14 | 18 | 22 |
+----+----+----+----+----+----+
| 3  | 7  | 11 | 15 | 19 | 23 |
+----+----+----+----+----+----+
| 4  | 8  | 12 | 16 | 20 | 24 |
+----+----+----+----+----+----+
| 25 | 29 | 33 | 37 | 41 | 45 |
+----+----+----+----+----+----+
| 26 | 30 | 34 | 38 | 42 | 46 |
+----+----+----+----+----+----+
| 27 | 31 | 35 | 39 | 43 | 47 |
+----+----+----+----+----+----+
| 28 | 32 | 36 | 40 | 44 | 48 |
+----+----+----+----+----+----+
int grid_width = 6; // Variable, but will always be a multiple of 2, to match subgroup width
int grid_height = 8;  // Variable, but will always be a multiple of 4, to match subgroup height
const int group_width = 2; // Constant, subgroup width is always 2.
const int group_height = 4; // Constant, subgroup height is always 4.

我想按照数字指示的顺序迭代这个1D输入向量/数组。(基本上是一个接一个地通过每个2x4子组)。 在循环内部,我想将当前处理的子组的8个元素加载到vector/int中,以便进一步处理。 最后我要处理6个子组,第一个是1到8,第二个是9到16,第四个是。。 这些组应该从左到右进行处理

注意:输入数据中的数字仅作为示例,以明确数据的处理顺序。输入数据可能包含完全随机的值。应保持每组中值的顺序。组内不应进行排序/重新排序。重要的是子组/数组维度等的约束

连同一维数组/向量,我确实知道网格的尺寸和子组的大小:

+----+----+----+----+----+----+
| 1  | 5  | 9  | 13 | 17 | 21 |
+----+----+----+----+----+----+
| 2  | 6  | 10 | 14 | 18 | 22 |
+----+----+----+----+----+----+
| 3  | 7  | 11 | 15 | 19 | 23 |
+----+----+----+----+----+----+
| 4  | 8  | 12 | 16 | 20 | 24 |
+----+----+----+----+----+----+
| 25 | 29 | 33 | 37 | 41 | 45 |
+----+----+----+----+----+----+
| 26 | 30 | 34 | 38 | 42 | 46 |
+----+----+----+----+----+----+
| 27 | 31 | 35 | 39 | 43 | 47 |
+----+----+----+----+----+----+
| 28 | 32 | 36 | 40 | 44 | 48 |
+----+----+----+----+----+----+
int grid_width = 6; // Variable, but will always be a multiple of 2, to match subgroup width
int grid_height = 8;  // Variable, but will always be a multiple of 4, to match subgroup height
const int group_width = 2; // Constant, subgroup width is always 2.
const int group_height = 4; // Constant, subgroup height is always 4.
我的想法是让一个循环遍历整个数据,然后两个循环相应地处理子组。但是我真的很难得到元素的循环条件和索引。我的思维过程是这样的:

// Iterate over entire data
for (int i = 0; i < grid_width * grid_height; i += (group_width * group_height))
{
    int block[8];
    // Iterate groups in correct order
    for (int j = 0; j < group_height; j++)
    {
        for (int k = 0; k < group_width; k++)
        {
            // Grab current element in group
            int value = input_array[i * grid_width + j]; // ?? This ain't right

            // Add element to group array/vector
            block[??] = ??;
        }
    }

    // Do further processing with the group array
}
//迭代整个数据
对于(int i=0;i<网格宽度*网格高度;i+=(组宽度*组高度))
{
int块[8];
//按正确的顺序迭代组
对于(int j=0;j
有人能帮我正确地循环这个吗?
我希望这个问题足够清楚。如果没有,请告诉我。

我想这就可以了:

编辑:我添加了这个解释,因为有些人到目前为止还不能理解这个解决方案,尽管它只包含一些简单的线性指数计算,而且我没有看到任何非TL;灾难恢复但完整的解决方案: 我们前后迭代输入数据。我们总是计算我们读取的值所属的索引。我们将值存储到适当的索引中。 群是二次块。我们使用
group\u position\u x
将我们的水平位置存储在每个组中。类似地,我们从左上角开始,对组内的垂直位置使用
group\u position\u y
。 我们使用
col
来表示我们在
col
th组中。 类似地,我们使用
来表示我们在列中的
第th组中。 然后,我们可以轻松地计算从输入读取的值必须存储在哪里。 该组是所有组中的第
col+[col size]*行。
以类似的方式,我们使用组内的x和y对应项来查看刚刚读取的值必须存储在组内的哪个索引处。
请注意,索引从0开始。另外,请参见下面代码中的注释。
的新值以及组内的坐标在每一步中递增。当然,在每种情况下,我们都会讨论整数的增量模。在某些情况下,如果满足其他索引的某些溢出条件,则会出现“
+1


using data_type = int // change type to correctly group data not being integers
using group = std::vector<data_type>; // reprensents the values of one group
const data_type DEFAULT{ -1 };

/**
@param data_begin begin iterator of the input data
@param data_end end iterator of the input data
@return a vector of all groups from top to bottom, from left to right.
*/
template<class _Const_Iterator>
std::vector<group> grouping(int grid_width, int grid_height, int group_width, int group_height, _Const_Iterator data_begin, _Const_Iterator data_end){
 const int GROUPS_PER_ROW{ grid_width / group_width };
 const int GROUPS_PER_COL{ grid_height / group_height };
 const int NUMBER_GROUPS{ GROUPS_PER_ROW * GROUPS_PER_COL };
 const int GROUP_SIZE{ group_width * group_height };
 std::vector<group> result(NUMBER_GROUPS, group(GROUP_SIZE, DEFAULT)); // create empty result vector with correct size.
 std::size_t group_index{ 0 }; // id of the current group
 std::size_t group_position_y{ 0 }; // currrent vertical position inside a group block
 std::size_t group_position_x{ 0 }; // current horizontal position inside a group block
 std::size_t row{ 0 }; // current row with respect to the groups
 std::size_t col{ 0 }; // current column with respect to the groups

 // iterate through all input data, copy data cell to target structur (result), calculate the correct indices for the next input data cell
 for (_Const_Iterator it{data_begin}; it != data_end; ++it){
  result[group_index][group_position_y + group_height * group_position_x] = *it;

  group_position_x = (group_position_x + 1) % group_width; // new horizontal position inside group
  auto old_col{ col };
  col = (col + (group_position_x == 0)) % GROUPS_PER_ROW; // new column with respect to the groups
  auto old_group_position_y{ group_position_y };
  group_position_y = (group_position_y  + (col == 0 && col != old_col)) % group_height; // new vertical position inside group
  row = (row + (group_position_y == 0 && group_position_y != old_group_position_y)) % GROUPS_PER_COL ; // new row with respect to the groups
  group_index = col + GROUPS_PER_ROW * row; 
 }
 return result;
}

使用data_type=int//更改类型以正确分组不是整数的数据
使用group=std::vector;//表示一个组的值
常量数据类型默认值{-1};
/**
@参数数据\u输入数据的begin begin迭代器
@参数数据\u输入数据的结束迭代器
@从上到下、从左到右返回所有组的向量。
*/
模板
向量分组(整数网格宽度、整数网格高度、整数组宽度、整数组高度、常数迭代器数据开始、常数迭代器数据结束){
const int GROUPS_PER_ROW{grid_width/group_width};
const int GROUPS_PER_COL{grid_height/group_height};
const int NUMBER_GROUPS{GROUPS_PER_ROW*GROUPS_PER_COL};
const int GROUP_SIZE{GROUP_width*GROUP_height};
std::vector result(NUMBER_GROUPS,group(group_SIZE,默认值));//创建大小正确的空结果向量。
std::size\u t group\u index{0};//当前组的id
std::size\u t group\u position\u y{0};//当前组块内的垂直位置
std::size\u t group\u position\u x{0};//组块内的当前水平位置
std::size\u t row{0};//关于组的当前行
std::size\u t col{0};//关于组的当前列
//迭代所有输入数据,将数据单元复制到目标结构(结果),为下一个输入数据单元计算正确的索引
for(_Const_Iterator it{data_begin};it!=data_end;++it){
结果【组位指数】【组位y+组位高度*组位x】=*it;
group_position_x=(group_position_x+1)%group_width;//组内新的水平位置
自动旧_col{col};
col=(col+(group_position_x==0))%GROUPS_PER_ROW;//关于组的新列
自动旧组位置{组位置};
group_position_y=(group_position_y+(col==0&&col!=old_col))%group_height;//组内的新垂直位置
行=(行+(组位置y==0和组位置y!=旧组位置y))%GROUPS\u PER\u COL;//关于组的新行
分组索引=列+每行分组*行;
}
返回结果;
}
在我个人看来,我