c++ - vector .push_back

标签 c++ vector push-back

我正在编写一个从给定格式的数据文件中读取的应用程序。在文件中,我动态创建了一个指向 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;行和后者打印。

如果你还没有猜到:

tempTestStringACCEL - 字符串对象

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/

相关文章:

c++ - QML TableView + PostgreSQL 数据库报错

c++ - 指针的 STL vector 上的等于运算符

c++ - 如何通过迭代器将 map<string, int> push_back 到 vector<map<string, int>> 中?

c++ - 如何将元素移动到 vector 的末尾?

c++ - c++中一行用户输入数组元素的相关问题

c++ - 从 C++ 调用 Delphi DLL 函数

c++ - 谁能帮我把这个结构翻译成delphi记录?

c++ - 在 linux 上运行 cygwin 编译的 c++ 程序

c++ - 通用 vector 类实现

vector - Agda:Stdlib 中的向量成员资格? (以及一般如何学习 stdlib)