繁体   English   中英

输出单元格值的简单代码在 C++ 中给出了运行时 SIGABRT 错误

[英]simple code outputting cell values gives a runtime SIGABRT error in C++

我已经实现了一个模拟细胞寿命的简单代码。 对于某些情况,它工作正常,但对于其他情况,它会在第 72 行(如突出显示)中给出 SIGABRT 超出范围错误。 该代码本质上将一个单元格编号向量作为输入。 然后它创建两个新向量,celllife 和 time 向量,在其中存储细胞的寿命和经过的时间以比较细胞的寿命。 例如,如果细胞生命向量是

{2, 3, 4, 1, 2}

和时间向量的元素是:

{0, 1, 2, 0, 1}

这意味着单元格 1 的生命周期为 2,但已经过去了 0 秒,依此类推。 该程序每秒打印细胞总数,并在所有细胞死亡时终止,即时间向量值大于细胞寿命值。 预期的输出应该是这样的:

[Main] Input gene seeds: 2 10 18 26 34 66
Monitor: Total cells: 0 [0 s]
Monitor: Total cells: 6 [1 s]
Monitor: Total cells: 24 [2 s]
Monitor: Total cells: 18 [3 s]
Monitor: Total cells: 0 [4 s]

但由于错误,它只能到达倒数第二行:

[Main] Input gene seeds: 2 10 18 26 34 66
Monitor: Total cells: 0 [0 s]
Monitor: Total cells: 6 [1 s]
Monitor: Total cells: 24 [2 s]
Monitor: Total cells: 18 [3 s]

任何修复错误的建议将不胜感激!

// code
#include <iostream>
#include <bits/stdc++.h> 
#include <thread>
#include <string>
#include <sstream>
#include<vector>
#include <chrono>
#include <mutex>
#include <stdlib.h>
using namespace std;
vector<int> inputs;
vector<thread> threads;
vector<int>childcells;
vector<int>celllife;
vector<int>cellvalues;
vector<int> timevector;
int laststrike= 0;
int newcell_lifetime;
//int counter;
int counter= 0;
int sum;
int main()
{   
    cout<<"[Main] Please input a list of gene seeds: "<<endl;
    //we get a line as string and pass them into the vector 'inputs'
    int value;
    string line;
    getline(cin, line);
    istringstream iss(line);
    while(iss >> value){

       inputs.push_back(value);
    }

    int total=0;
    int timer= 0;
    bool happening = true;
    cout<<"[Monitor] Started [ 0 s ]"<<endl;  //intial value of total cells
    while(happening == true){//this runs until the total value reaches zero
        int j=0;

        for (int unsigned  i = 0; i < inputs.size(); i++) {
            //traverses through every element in vector
            //the following are computations for calculating variables 'lifetime' and 
 'halflife' 
            int lifetime = (0.1+(inputs.at(i)%8));
            if (timer >= lifetime ){


                inputs.erase(inputs.begin() + i);

                i--;
                continue;
            }
            j=j+1;

            int halflife= lifetime/2;
                if (timer == halflife ){
                int newcell = inputs.at(i)/ 8;
                if(newcell != 0){
                //newcells=newcells+ newcell;
                childcells.push_back(newcell);
                celllife.push_back(lifetime);
                timevector.push_back(0);

                }//if halflife equals timer, the code inserts the new cell into the childcells vector and inserts a 0 value into the time vector
            }


        }



        int count=0;
        vector<int>::iterator it;
        for(it = celllife.begin(); it != celllife.end(); it++,count++ ){

                if (celllife.at(count) == timevector.at(count)) { //this is the line with the SIGABRT error
                    timevector.erase(timevector.begin() + count);
                    celllife.erase(celllife.begin() + count);
                    childcells.erase(childcells.begin() + count);

            }

            }
        counter++;
        sum= accumulate(childcells.begin(), childcells.end(), 0); 

        total=j+sum;

        timer=timer+1;
         cout<<"[Monitor] Total cells "<<total<<" [ " <<int(timer) <<" s ]"<<endl;

        for (int unsigned  k = 0;  k< timevector.size(); k++) {
            timevector.at(k)++;

        }//after one second, all timevector values are incremented
        if(total == 0){

            happening = false;
        }

    }

    return 0;

}
 celllife.erase(celllife.begin() + count);

在您的 C++ 书中的某处,您会发现std::vector::erase将使满足以下条件的迭代器无效的解释:

在擦除点或之后使迭代器和引用无效,包括 end() 迭代器。

for(it = celllife.begin(); it != celllife.end(); it++,count++ ){

这方面的一个仔细检查for循环,与上面提到的erase()调用,显示, erase()总是会删除被引用的同一个值it的迭代器,在这里。 因此,在erase()调用此之后it它将不再有效,并且递增无效it迭代器,作为循环迭代表达式的一部分,会导致未定义的行为,并且肯定是导致崩溃的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM