掼蛋网站建设,微信支持什么wordpress,wordpress怎么设置用户收费,快速开发平台开发一, 逐个点的赋值(单个循环)
一维数组又[56],是一个30个元素的数组,将他赋值给一个[56]五行六列的二维矩阵中,一位数组和二维矩阵的 坐标转换:[i/列数][i%列数] // 赋值给二维矩阵// i从0~一维数组个个数,仅一个循环for (int i 0; i rows * cols; i){matrix[i / cols][i…一, 逐个点的赋值(单个循环)
一维数组又[56],是一个30个元素的数组,将他赋值给一个[56]五行六列的二维矩阵中,一位数组和二维矩阵的 坐标转换:[i/列数][i%列数] // 赋值给二维矩阵// i从0~一维数组个个数,仅一个循环for (int i 0; i rows * cols; i){matrix[i / cols][i % cols] one_matrix[i]; 坐标转换[i/列数][i%列数]}示例:
int main()
{int rows 5; // 行数int cols 6; // 列数int* one_matrix new int[rows * cols]; // 申请一维数组空间for (int i 0; i rows * cols; i){one_matrix[i] i;}std::vectorstd::vectorint matrix;matrix.resize(rows,std::vectorint(cols)); // 申请二维数组空间// 赋值给二维矩阵for (int i 0; i rows * cols; i){matrix[i / cols][i % cols] one_matrix[i]; // 坐标转换[i/列数][i%列数]}// 打印查看for (int i 0; i rows ; i){for (int j 0; j cols ; j){std::cout matrix[i][j] ;}std::cout std::endl;}delete[] one_matrix;return 0;
}打印查看
0 1 2 3 4 5
6 7 8 9 10 11
12 13 14 15 16 17
18 19 20 21 22 23
24 25 26 27 28 29二,使用memcpy方式赋值 // 使用memcpy进行赋值for (int i 0; i rows; i){memcpy(matrix[i].data(), one_matrix[i * cols], cols * sizeof(i));// 或者memcpy(matrix[i][0], one_matrix[i * cols], cols * sizeof(i));}int main()
{int rows 5;int cols 6;int* one_matrix new int[rows * cols];for (int i 0; i rows * cols; i){one_matrix[i] i;}std::vectorstd::vectorint matrix;matrix.resize(rows,std::vectorint(cols));// 使用memcpy进行赋值for (int i 0; i rows; i){memcpy(matrix[i].data(), one_matrix[i * cols], cols * sizeof(i));// 或者memcpy(matrix[i][0], one_matrix[i * cols], cols * sizeof(i));}//打印查看for (int i 0; i rows ; i){for (int j 0; j cols ; j){std::cout matrix[i][j] ;}std::cout std::endl;}delete[] one_matrix;return 0;
}