首页 > 解决方案 > 堆栈 std::vectors 以生成矩阵,类似于 numpy.vstack

问题描述

有没有办法堆叠,例如,两个std::vectors大小为 N,以生成每 N 列 2 行的矩阵?类似于 Python 的 np.vstack 函数?不使用低效的vectors ( std::vector<std::vector<type>>) 解决方案或array 数组?

标签: c++

解决方案


只需使用一维数组。多维数组语法是邪恶的。

const int width = 1024;
const int height = 768;
vector<float> matrix(0,width*height);

float getCell(int row, int col)
{
    return matrix[row * width + col];
}

float setCell(int row, int col, float val)
{
    matrix[row * width + col] = val;
}

推荐阅读