【问题标题】:"Deque iterator not dereferencable" Error“Deque 迭代器不可取消引用”错误
【发布时间】:2019-04-13 20:44:49
【问题描述】:

我正在尝试遍历双端队列并擦除到达时间最短的元素。该函数前几次工作,然后给我“迭代器不可取消引用”错误并停止工作。

我的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();
};

我的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;
}

最后,我在哪里调用函数:

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() 成功运行两到三遍然后给我错误。任何帮助将不胜感激。

【问题讨论】:

  • while (iter != priorityQueue.end()),小于等于的比较通常在迭代器之间没有意义,而等于意味着您可以取消引用结束迭代器,这将导致您看到的错误。
  • 坏主意iter &lt;= priorityQueue.end(),如果双端队列为空,则返回true,因此您正在访问不存在的项目字段。

标签: c++


【解决方案1】:

在 C++ 中,结束迭代器“指向”最后一个元素之后的位置。

findNextJob 允许迭代器 iter 变为等于 priorityQueue.end(),因此取消引用它是无效的。

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

【讨论】:

  • 所以我将条件更改为while (iter != priorityQueue.end()),现在我得到一个“双端迭代器+偏移超出范围”。这真的让我很困惑,我找不到我做错了什么。
  • @Hasan 考虑一下如果priorityQueue 中的第一个工作是时间最短的工作会发生什么。
  • @1201ProgramAlarm 感谢您的评论!有效!我根本没有考虑这个案子。我用deque&lt;Job&gt;::iterator location = iter; 初始化了location,解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 2015-01-13
  • 2018-02-01
  • 2013-06-03
  • 2017-01-16
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多