C++11 基于二维向量的断层分割
我有一个包含表格式值的文件。文件中的行数和列数可能会有所不同C++11 基于二维向量的断层分割,c++11,vector,segmentation-fault,C++11,Vector,Segmentation Fault,我有一个包含表格式值的文件。文件中的行数和列数可能会有所不同 33829731.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 205282038.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -
33829731.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00
205282038.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00
3021548.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00
203294496.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00
205420417.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00
我使用一个二维向量来存储数据,使用下面的代码
ifstream inputCent("file.txt");
std::vector<std::vector<double> > C;
std::vector<double> col(15);
while(!inputCent.eof())
{
for(int i = 0; i < col.size(); i++)
{
inputCent >> col[i];
C[i].push_back(col[i]);
}
}
ifstream输入(“file.txt”);
std::向量C;
std::向量col(15);
而(!inputCent.eof())
{
对于(int i=0;i>列[i];
C[i]。推回(col[i]);
}
}
但这给了我
分段错误:11
。然而,如果我初始化std::vector C(15)代码>像这样,它可以工作15行。但正如我所说,行数可能会有所不同。为什么我必须初始化C
的大小?或者我做错了什么 您试图将_向后推
到一个可能不存在的向量。。。正确的代码如下:
ifstream inputCent("file.txt");
std::vector<std::vector<double> > C;
std::vector<double> col(15);
while(!inputCent.eof())
{
for(int i = 0; i < col.size(); i++)
{
inputCent >> col[i];
}
C.push_back(col);
}
ifstream输入(“file.txt”);
std::向量C;
std::向量col(15);
而(!inputCent.eof())
{
对于(int i=0;i>列[i];
}
C.推回(col);
}
如上所示,在将整个col
向量推到C
的后面之前,用值填充col
向量更有意义。您试图将推回到一个可能不存在的向量。。。正确的代码如下:
ifstream inputCent("file.txt");
std::vector<std::vector<double> > C;
std::vector<double> col(15);
while(!inputCent.eof())
{
for(int i = 0; i < col.size(); i++)
{
inputCent >> col[i];
}
C.push_back(col);
}
ifstream输入(“file.txt”);
std::向量C;
std::向量col(15);
而(!inputCent.eof())
{
对于(int i=0;i>列[i];
}
C.推回(col);
}
如上所示,在将整个列
向量推到C
的后面之前,用值填充列
向量更有意义,谢谢。:)愚蠢的错误。明白了,谢谢。:)愚蠢的错误。