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

标签 c++ map stl iterator

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

我们正在做一个项目: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' was not declared in this scope.

我们也试过:

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

代码块回答:

error: 'std::map, void (Interpreteur::*)()>::iterator' has no member named 'second'

如果有人能帮助我们,这将挽救我们的项目,我们必须在明天结束它。我们将不胜感激。

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

最佳答案

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

trouve->second

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

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

关于c++ - 如何访问第二个 map 迭代器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24057034/

相关文章:

c++ - 使用 find 方法后如何更新 std::map ?

c++ - 如何在 std::vector 中生成对象而不复制?

c++ - 在 C++ 中解析这个的最好方法是什么?

c++ - 智能指针问题

map - hadoop过滤 map 输出

c++ - 在不知道 C++ 模板类型的情况下调用超模板类方法

C++ 映射迭代器?

c++ - 位集超过 32 位?

c++ - 类和继承

c++ - COM引用计数问题