【发布时间】:2017-03-01 15:47:48
【问题描述】:
我有一个包含表格格式值的文件。文件中的行数和列数可能会有所不同。
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]);
}
}
但这给了我Segmentation fault: 11。但是,如果我像这样初始化std::vector<std::vector<double> > C(15);,那么它适用于 15 行。但正如我所说,行数可能会有所不同。为什么一定要初始化C的大小?或者我做错了什么?
【问题讨论】:
标签: c++11 vector segmentation-fault