提问者:小点点

用向量作矩阵? [副本]


在C++11中,我如何定义一个向量,使其大小为4*5,这样我就可以像对待一个矩阵一样对待它呢? (我的意思是使用运算符[],如下所示)

mat[2][3];

更新:下面给出了错误:

#include <memory>
class Test{
        std::vector<int> vect;
};

错误消息:

implicit instantiation of undefined template 'std::__1::vector<int, std::__1::allocator<int> >'
    std::vector<int> vect;

共1个答案

匿名用户

您只需要一个嵌套的向量,如下所示:

auto v = std::vector<std::vector<int>>(4, std::vector<int>(5));

然后可以像v[0][0]那样对其进行索引。