map <int, string> rollCallRegister;
map <int, string> :: iterator rollCallRegisterIter;
pair <map <int, string> , bool> returnPair;
rollCallRegister.insert (pair <int, string> (1, "anisha"));
In this code, pair <map <int, string> , bool> returnPair; means that this pair takes a map row as the first value and a bool as the second.
Question:
How to insert bool here: rollCallRegister.insert (pair <int, string> (1, "anisha"));?
Secondly, pair <map <int, string> :: iterator, bool> returnPair; This pair takes an iterator of map as the first input.
Question:
How is this different from the previous pair syntax, since the insertion way is still the same: rollCallRegister.insert (pair <int, string> (1, "anisha"));?
The first value of a
pair<map<int, string >, bool>is not a map row, it is an entire map (so probably not what you are looking for). The second pair, on the other hand, associates a map entry to a boolean value.Regarding the insertion, I don’t really get your question: in both samples, you are inserting into a
map<int, string>; it has nothing to do with the different types of pairs you define. To create instances of these two kind of pairs, you need amapin the first case, and an iterator in the second:Edit:
Based on the comments you made on your question, I think you confuse the content of the map (
pair<string, int>) and the value returned byinsert(pair<map<string, int>::iterator, bool>).When you declare a
map<K,V>, its content is stored inpair<K,V>. Therefore, to insert a new entry in this map, you need to create a pair containing the key and the value you want to insert:Now, when you insert an entry into a map, there is a possibility that the key was already present. If this is the case, then the insertion should “failed”: after the call to
insert, the key is still associated with the former value. However, the caller should be warned that he tried to insert an entry with a key that already existed in the map.This is achieved by having
insertreturn apair<map<K,V>::iterator, bool>, where the second value of this pair is a boolean indicating whether the insertion occurred (the key was not already present in the map) or not. The first value is an iterator to the entry corresponding to the key. This entry contains the key and its associated value (either the one you just inserted, or the one that was already there).