<代码>pair<map<int, string > bool>的第一个价值是not地图浏览,它是整个地图(可能不是你所期待的)。 而另一方面,第二位奶牛把地图输入到一个毛豆价值中。
关于插入,我没有真正获得你的问题:在这两种样本中,你都插入了<条码>地图和带;int, string >; 它与你定义的不同类型的奶制品毫无关系。 为了制造这两种奶制品,您首先需要>map
,在第二种情况下需要一名导师:
pair<map<string, int>, bool> p1(rollCallRegister, true);
pair<map<string, int>::iterator, bool> p2(rollCallRegisterIter, false);
Edit:
根据你对你的提问所作的评论,我认为你将地图的内容(pair<string, int>
sert(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).