【问题标题】:How to access to the second map iterator?如何访问第二个地图迭代器?
【发布时间】:2014-07-26 06:14:41
【问题描述】:

我们是两个学生,现在我们遇到了一个无法解决的大问题。我们向我们的老师寻求帮助,但他无法帮助我们,所以我们最后的机会就是这个论坛!

我们正在做一个项目:NPI 文件的命令解释器

map<string,void(Interpreteur::*)()>::iterator trouve = interpreteur.myMap.find(saisie);
if(trouve == interpreteur.myMap.end()) 
    cerr<<"command not found"<<endl; 
else 
    (trouve->*second)();

我们必须使用名为“map”的对象,但我们无法获得第二个参数,名为..“Second”。为什么?代码块告诉我们错误在“else”中,这里是错误:

'second' 未在此范围内声明。

我们也试过了:

map<string,void(Interpreteur::*)()>::iterator trouve = interpreteur.myMap.find(saisie);
if(trouve == interpreteur.myMap.end()) 
    cerr<<"command not found"<<endl; 
else 
    (trouve.second)();

并且代码块得到了回答:

错误:'std::map, void (Interpreteur::*)()>::iterator' 没有名为 'second' 的成员

如果有人可以帮助我们,它将拯救我们的项目,我们必须在明天结束它。我们将非常感激。

非常感谢您的帮助,如果有问题我们可以回答:)

【问题讨论】:

    标签: c++ map stl iterator


    【解决方案1】:

    std::map 迭代器指向一对。因此,要访问该对的第二个元素,请执行以下操作:

    trouve->second
    

    请注意,在您的情况下,第二个元素的类型是“指向Interpreteur 的成员函数的指针”,因此要调用它,您需要提供一个Interpreteur 对象。像这样的:

    (interpreteur.*(trouve->second))()
    

    【讨论】:

      猜你喜欢
      • 2010-12-02
      • 2012-08-04
      • 2019-04-26
      • 2013-01-05
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 2014-09-21
      相关资源
      最近更新 更多