简体   繁体   中英

STL pair input with STL map

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")); ?

You can't also insert a bool into rollCallRegister since it takes only a int as key and a string as value in your current form.

If you wish the rollCallRegister map to contain a pair of (int, string) as key and a bool as value you need to change it to:

map <pair<int, string>, bool> rollCallRegister;
rollCallRegister.insert(std::make_pair(std::make_pair(yourint, yourstring), true/false));

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 a map in the first case, and an iterator in the second:

pair<map<string, int>, bool> p1(rollCallRegister, true);
pair<map<string, int>::iterator, bool> p2(rollCallRegisterIter, false);

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 by insert ( pair<map<string, int>::iterator, bool> ).

When you declare a map<K,V> , its content is stored in pair<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:

map<K,V> myMap;
pair<K,V> myEntry(key, value); // entry to insert

myMap.insert(myEntry);         //or you can create the entry on-the-fly
myMap.insert(make_pair(key, value));

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 insert return a pair<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).

Fairly trivial: pair<pair <int, string>, bool> . You might want to look into make_pair though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM