向量下标超出范围多维向量
Vector subscript out of range multidimensional vector
我正在处理 PLY 文件 reader(包含顶点和面位置信息的 txt 文件)。它由 header、顶点位置、面信息组成。
header
-1 -1 -1 // x y z 坐标
3 1 2 3 // 这个面由 3 个顶点组成 - 1,2,3
之后我用OpenGL绘制它。
我让它与数组一起工作,现在我想使用矢量容器来节省价值。因为我不必在编译期间知道大小。
我初始化我的两个向量:
vector<string> ply_file;
vector< vector<float> > vertex_list;
使用 ply_file 一切正常,但是当我尝试使用此代码写入 vertex_list 时:
int j = 0;
for(int i=first_vertex_row-1;i<first_vertex_row+vertex_number-1;i++)
{
std::stringstream ss(ply_file[i]);
ss >> vertex_list[j][0] >> vertex_list[j][1] >> vertex_list[j][2]; // x,y,z
j++;
}
但我得到调试断言失败:矢量下标超出范围。
我知道我写错了,但我无法让它工作。
是否可以用stringstream将它写入多维向量?如果可以,如何实现?
vector< vector<float> > vertex_list;
创建一个空向量。当您尝试使用
向其中插入值时
ss >> vertex_list[j][0] >> vertex_list[j][1] >> vertex_list[j][2];
所有这些索引都超出了向量的范围。为了像这样向向量添加值,您需要将其构造为您想要的大小。对于二维向量,它将具有
的形式
std::vector<std::vector<some_type> some_name(rows, std::vector<some_type>(columns, value));
我正在处理 PLY 文件 reader(包含顶点和面位置信息的 txt 文件)。它由 header、顶点位置、面信息组成。
header
-1 -1 -1 // x y z 坐标
3 1 2 3 // 这个面由 3 个顶点组成 - 1,2,3
之后我用OpenGL绘制它。
我让它与数组一起工作,现在我想使用矢量容器来节省价值。因为我不必在编译期间知道大小。
我初始化我的两个向量:
vector<string> ply_file;
vector< vector<float> > vertex_list;
使用 ply_file 一切正常,但是当我尝试使用此代码写入 vertex_list 时:
int j = 0;
for(int i=first_vertex_row-1;i<first_vertex_row+vertex_number-1;i++)
{
std::stringstream ss(ply_file[i]);
ss >> vertex_list[j][0] >> vertex_list[j][1] >> vertex_list[j][2]; // x,y,z
j++;
}
但我得到调试断言失败:矢量下标超出范围。
我知道我写错了,但我无法让它工作。 是否可以用stringstream将它写入多维向量?如果可以,如何实现?
vector< vector<float> > vertex_list;
创建一个空向量。当您尝试使用
ss >> vertex_list[j][0] >> vertex_list[j][1] >> vertex_list[j][2];
所有这些索引都超出了向量的范围。为了像这样向向量添加值,您需要将其构造为您想要的大小。对于二维向量,它将具有
的形式std::vector<std::vector<some_type> some_name(rows, std::vector<some_type>(columns, value));