我正在编写一个从给定格式的数据文件中读取的应用程序。在文件中,我动态创建了一个指向 vector 对象的二维指针数组。基本上,它会读取整个文件,当它找到给定的字符串模式时,它会停止并读取
while(getline(inputFile,tempTestString)){
// first for ACCEL
if(string::npos != tempTestString.find(ACCEL)){
sstream.str(tempTestString);
sstream >> pushBack;
cout << "position 1" << endl;
array[tempDim1][tempDim2].vectorName->push_back(pushBack);
cout << "position 2" << endl;
break;
}
}
现在,pushBack 是一个很大的数字,可能高达 20000,但它因文件而异。
此代码的问题是我没有收到任何运行时错误,甚至没有抛出任何异常,我 try catch 它们。程序简单地结束了!可以肯定的是,我添加了 cout << "position1" << endl;
和 cout << "position2" << endl;
行和后者打印。
如果你还没有猜到:
tempTestString
和 ACCEL
- 字符串对象
sstream
- 字符串流对象
array
- 动态内存中的二维结构数组
vectorName
- 指向 vector 对象的指针,由 array
指向的结构成员
附录:
因此,为了回应一些评论,这里是代码的另一部分,其中创建了所有变量:
数组
array = new structName* [tempDim1];
for(int i = 0; i < tempDim2; i++){
array[i] = new structName [tempDim2];
}
结构名称
struct structName{
vector<double>* vectorName;
vector<double>* vectorName1;
vector<double>* vectorName2;
};
tempDim1 和 tempDim2 都是 const ints
,分别为 2 和 3。 pushBack 的值最大可达 20000
最佳答案
尝试更正此问题:
array = new structName* [tempDim1];
for(int i = 0; i < tempDim2; i++){
array[i] = new structName [tempDim2];
}
=>
array = new structName* [tempDim1];
for(int i = 0; i < tempDim1; i++){
array[i] = new structName [tempDim2];
}
关于c++ - vector .push_back,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14971890/