简体   繁体   English

STL对输入STL映射

[英]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; 在这段代码中, pair <map <int, string> , bool> returnPair; means that this pair takes a map row as the first value and a bool as the second. 表示该对将地图行作为第一个值,将bool作为第二个值。

Question : 问题
How to insert bool here: rollCallRegister.insert (pair <int, string> (1, "anisha")); 如何在这里插入bool: rollCallRegister.insert (pair <int, string> (1, "anisha")); ?

Secondly, pair <map <int, string> :: iterator, bool> returnPair; 其次, pair <map <int, string> :: iterator, bool> returnPair; This pair takes an iterator of map as the first input. 该对将map的迭代器作为第一个输入。

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")); 这与前一对语法有何不同,因为插入方式仍然相同: 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. 您也不能将 bool插入rollCallRegister,因为它只需要一个int作为键,一个字符串作为当前表单中的值。

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: 如果您希望rollCallRegister映射包含一对(int,string)作为键,并且bool作为值 ,则需要将其更改为:

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). pair<map<int, string >, bool>的第一个值不是地图行,它是整个地图(所以可能不是你想要的)。 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> ; 关于插入,我真的没有得到你的问题:在两个样本中,你插入到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: 要创建这两种对的实例,您需要在第一种情况下使用map ,在第二种情况下使用迭代器:

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> ). 根据您对问题的评论,我认为您混淆了地图的内容( pair<string, int> )和insert返回的值( pair<map<string, int>::iterator, bool> )。

When you declare a map<K,V> , its content is stored in pair<K,V> . 声明map<K,V> ,其内容存储在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. 如果是这种情况,那么插入应该“失败”:在调用insert ,该键仍然与前一个值相关联。 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. 这是通过让insert返回一pair<map<K,V>::iterator, bool> ,其中该对的第二个值是一个布尔值,表示是否发生了插入(该键尚未出现在地图中)或不。 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> . 相当琐碎: pair<pair <int, string>, bool> You might want to look into make_pair though. 您可能希望查看make_pair

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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