简体   繁体   English

“双端队列迭代器不可引用”错误

[英]“Deque iterator not dereferencable” Error

I'm trying to iterate through a deque and erase the element with the smallest arrivalTime. 我正在尝试遍历双端队列并以最小的到达时间擦除元素。 The function works the first couple of times, then gives me the "iterator not dereferencable" error and stops working. 该函数在前几次运行,然后显示“迭代器不可解除”错误并停止工作。

My Processor.h : 我的Processor.h

#pragma once

#include <deque>
#include "Job.h"
#include <ctime>

using namespace std;

class Processor {
public:
    deque<Job> priorityQueue;
    Job runningJob;
    bool isProcessing;
    int processingTime;
    int runningTime;
    int overallJobs;
    int numJobs[4];     //0 is A, 1 is B, 2 is C, 3 is D
    int runningJobNumber;
    int interruptedJobs;

    Processor();

    void simulate();

    Job findNextJob();
};

My findNextJob() : 我的findNextJob()

Job Processor::findNextJob() {
    Job nextJob = priorityQueue.front();

    deque<Job>::const_iterator iter = priorityQueue.begin();
    deque<Job>::const_iterator location;

    while (iter <= priorityQueue.end()) {
        if (iter->arrivalTime < nextJob.arrivalTime) {
            nextJob = *iter;
            location = iter;
            iter++;
        }
        else {
            iter++;
        }
    }

    priorityQueue.erase(location);

    return nextJob;
}

And finally, where I'm calling the function: 最后,在我调用该函数的地方:

void Processor::simulate() {
    srand(time(NULL));

    char newJobType;

    while (processingTime <= 50) {
        newJobType = 65 + rand() % 4;
        Job newJob(newJobType);
        newJob.arrivalTime += runningTime;

        processingTime += newJob.processingTime;

        priorityQueue.push_back(newJob);
        cout << endl << newJob.type << " " << newJob.arrivalTime;
        runningTime++;
    }
    runningTime = 0;

    int jobFinishTime;
    Job nextJob;

    nextJob = findNextJob();

    while (priorityQueue.size()) {
        cout << endl << runningTime << ") " << "Queue size: " << priorityQueue.size();
        if (!isProcessing) {
            cout << " CPU 1 Idle;";

            if (nextJob.arrivalTime == runningTime) {
                runningJob = nextJob;

                //irrelevant code here

                nextJob = findNextJob();
            }
        }

        if (isProcessing && (runningJob.arrivalTime + runningJob.processingTime) != runningTime) {

            //irrelevant code here

            if (nextJob.arrivalTime <= runningTime) {
                runningJob = nextJob;

                //irrelevant code here

                nextJob = findNextJob();
            }
        }
        if (nextJob.priority > runningJob.priority && nextJob.arrivalTime <= runningTime) {
                //irrelevant code here
        }



        runningTime++;
    }



}

findNextJob() runs successfully two or three times then gives me the error. findNextJob()成功运行两次或三次,然后出现错误。 Any help would be appreciated. 任何帮助,将不胜感激。

In C++, the end iterator "points to" one past the last element. 在C ++中,结束迭代器“指向”最后一个元素之后的一个。

findNextJob allows the iterator iter to become equal to priorityQueue.end() , so dereferencing it is invalid. findNextJob允许迭代器iter变得等于priorityQueue.end() ,因此取消引用是无效的。

while (iter <= priorityQueue.end()) {
    // ...
}

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

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