Iterator and map

Hi guys , I've a map :
map<string,Question> mapQuest;
In which Question is a class defined in this way :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 public:
        Question(){};
        Question( string id , string text ){
            this->id = id ;
            this->text = text;
            priority = false;
        };
        // getter and setter
        .....
        //
        private:
        string id;
        string text;
        bool priority;

I would like to make a function that display the text of all the elements of the map, and after , to changing the value of priority to true.
I tried something like this, but it doesn't work:

1
2
3
4
5
6
7
8
 map<string,Question>::iterator it ;

for ( it = mapQuest.begin() ; it != mapQuest.end() ; ++it ){

     cout<< it->getText()<<endl;
     it->setPriority(true);
}

Last edited on
The map elements are stored in std::pair. http://www.cplusplus.com/reference/utility/pair/

it->first gives you the key value (the string).
it->second gives you the mapped value (the Question).

1
2
3
4
5
for ( it = mapQuest.begin() ; it != mapQuest.end() ; ++it ){
	Question& q = it->second;
	cout << q.getText() << endl;
	q.setPriority(true);
}

Last edited on
Topic archived. No new replies allowed.