运行时错误:映射/设置迭代器不兼容

发布于 2024-09-24 08:56:10 字数 753 浏览 4 评论 0原文

我在第 8 行遇到运行时错误“映射/设置迭代器不兼容”。

void Manager::Simulate(Military* military, Shalishut* shalishut,char* args[]){
    Simulation* simulation = Simulation::GetInstance();
    Time* time = Time::GetInstance();

    multimap<int,Task*>::iterator itTasks;
    itTasks = simulation->GetTasks().begin();
    while(itTasks != simulation->GetTasks().end()){
      while (itTasks->second->GetTimeStamp() == time->GetTime()){ /*line 8 - ERROR*/
            TaskExecute(itTasks->second,military,shalishut,args);
            itTasks++;
        }
        // Unit take car of vehicles
        time->TimeIncrease();
    }

}

Simulation 被声明为 multimap。问题是什么?

I have a runtime error "map/set iterators incompatible" at line 8.

void Manager::Simulate(Military* military, Shalishut* shalishut,char* args[]){
    Simulation* simulation = Simulation::GetInstance();
    Time* time = Time::GetInstance();

    multimap<int,Task*>::iterator itTasks;
    itTasks = simulation->GetTasks().begin();
    while(itTasks != simulation->GetTasks().end()){
      while (itTasks->second->GetTimeStamp() == time->GetTime()){ /*line 8 - ERROR*/
            TaskExecute(itTasks->second,military,shalishut,args);
            itTasks++;
        }
        // Unit take car of vehicles
        time->TimeIncrease();
    }

}

Simulation is declared as a multimap<int,Task*>. What is the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Hello爱情风 2024-10-01 08:56:10

我将大胆猜测一下 Simulation::GetTasks() 签名如下所示:

multimap<int,Task*> GetTasks() const;

每次您创建一个新的多重映射(一个副本)调用它。

比较迭代器时,两个 multimap 迭代器必须来自同一个容器;由于每次调用 GetTasks() 时都会获得一个新副本,因此违反了此​​约束,这就是错误的根源。您还有另一个问题 - 临时多重映射副本在创建它们的语句后被销毁,因此您的迭代器立即失效。

你有两个选择;一种是在本地捕获副本并一致地使用该副本:

multimap<int,Task*> tasks = simulation->GetTasks();
multimap<int,Task*>::iterator itTasks;
itTasks = tasks.begin();
while(itTasks != tasks.end()){
  while (itTasks->second->GetTimeStamp() == time->GetTime()){
        TaskExecute(itTasks->second,military,shalishut,args);
        itTasks++;
    }
    // Unit take car of vehicles
    time->TimeIncrease();
}

另一种是让 GetTasks() 返回对持久多重映射的引用,确保每次都使用相同的多重映射:

multimap<int,Task*> &GetTasks();

或常量引用:

const multimap<int,Task*> &GetTasks() const;

这具有避免复制multimap的(可能很大的)开销的优点。

请注意,使用 const 引用需要使用 const_iterator 来逐步执行多重映射。我建议同时定义 const 和非常量访问器(C++ 将根据 Simulation 指针或引用是否为 const 选择正确的访问器),除非您想禁止直接修改底层 multimap 完全,在这种情况下,您只能定义 const 变体。

I'm going to take a wild guess and say that the Simulation::GetTasks() signature looks like this:

multimap<int,Task*> GetTasks() const;

This creates a new multimap (a copy) each time you call it.

When comparing iterators, both of the multimap<int,Task*> iterators must come from the same container; since you're getting a new copy each time you call GetTasks(), you violate this constraint, and this is the source of your error. You also have another problem - the temporary multimap copies are destroyed after the statement they're created in, so your iterators are invalidated instantly.

You have two choices; one is to capture a copy locally and use that copy consistently:

multimap<int,Task*> tasks = simulation->GetTasks();
multimap<int,Task*>::iterator itTasks;
itTasks = tasks.begin();
while(itTasks != tasks.end()){
  while (itTasks->second->GetTimeStamp() == time->GetTime()){
        TaskExecute(itTasks->second,military,shalishut,args);
        itTasks++;
    }
    // Unit take car of vehicles
    time->TimeIncrease();
}

Another is to have GetTasks() return a reference to a persistent multimap, ensuring the same one is used each time:

multimap<int,Task*> &GetTasks();

Or a const reference:

const multimap<int,Task*> &GetTasks() const;

This has the advantage of avoiding the (potentially large) overhead of copying the multimap.

Note that using a const reference requires using const_iterators to step through the multimap. I would recommend defining both const and non-const accessors (C++ will pick the right one based on if the Simulation pointer or reference is const), unless you want to disallow direct modification of the underlying multimap entirely, in which case you can define only the const variant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文