Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access to the second map iterator?

We are two students and we now have an epic big problem that we can't resolve. We asked our teacher for some help but he can't help us, so our last chance is this forum!

We're doing a project: a command interpreter of NPI files.

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

We must use the object named "map" but we can't get the second parameter, named.. "Second". Why? Code Blocks told us the error is in the "else", here is the error:

'second' was not declared in this scope.

We have tried too:

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

And code blocks answered:

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

If someone can help us, it will save our project, we must end it for tomorrow.. We will be very grateful.

Thank you very much for help, we can answer questions, if there are any :)

like image 999
Jérémy Avatar asked Jun 05 '14 09:06

Jérémy


People also ask

What does iterator -> Second mean?

Refers to the first ( const ) element of the pair object pointed to by the iterator - i.e. it refers to a key in the map. Instead, the expression: i->second.

How do I find the first element of a map?

To get the first element of a Map , use destructuring assignment, e.g. const [firstKey] = map. keys() and const [firstValue] = map. values() . The keys() and values() methods return an iterator object that contains the Map's keys and values.


1 Answers

A std::map iterator points to a pair. So to access the pair's second element, do this:

trouve->second

Note that in your case, the type of that second element is "pointer to member function of Interpreteur," so to call it, you need to provide an Interpreteur object. Something like this:

(interpreteur.*(trouve->second))()
like image 92
Angew is no longer proud of SO Avatar answered Sep 30 '22 15:09

Angew is no longer proud of SO