// 創(chuàng)建并初始化一個二維數(shù)組
std::vector<std::vector<float>> createAndInitializeArray(int rows, int cols)
{
std::vector<std::vector<float>> array(rows, std::vector<float>(cols));
float value = 0.0f;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
array[i][j] = value;
value += 1.0f;
}
}
return array;
}
// 將二維數(shù)組的數(shù)據(jù)復制到一維數(shù)組并返回指針
float* flatten2DArray(std::vector<std::vector<float>>& inputArray)
{
int rows = inputArray.size();
int cols = inputArray[0].size();
float* flattenedArray = new float[rows * cols];
for (int i = 0; i < rows; i++)
{
std::memcpy(flattenedArray + i * cols, inputArray[i].data(), cols * sizeof(float));
}
return flattenedArray;
}
int main() {
int rows = 360;
int cols = 1000;
// 造假數(shù)據(jù)并將二維數(shù)組的數(shù)據(jù)復制到一維數(shù)組
std::vector<std::vector<float>> FalseMatrix = createAndInitializeArray(rows, cols);
float* fdata = flatten2DArray(FalseMatrix);
// 使用 fdata
delete [] fdata; // 當您不再需要該數(shù)組時,務必執(zhí)行釋放內(nèi)存的操作
return 0;
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-760861.html
文章來源:http://www.zghlxwxcb.cn/news/detail-760861.html
到了這里,關于造一個float類型二維矩陣,并將二維矩陣存快速儲到一個float*中(memcpy)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!