如何访问第二个map迭代器?

Jér*_*émy 6 c++ iterator stl map

我们是两个学生,现在我们有一个我们无法解决的史诗般的大问题.我们向老师请了一些帮助,但他无法帮助我们,所以我们最后一次机会就是这个论坛!

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

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

我们必须使用名为"map"的对象,但是我们不能得到名为"Second"的第二个参数.为什么?Code Blocks告诉我们错误是在"else"中,这是错误:

在此范围内未声明"秒".

我们也尝试过:

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

代码块回答:

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

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

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

Rei*_*ica 5

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

trouve->second
Run Code Online (Sandbox Code Playgroud)

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

(interpreteur.*(trouve->second))()
Run Code Online (Sandbox Code Playgroud)