English 中文(简体)
STL pair submissions with STLmap
原标题: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"));

在这项法典中,pair <map <int, string> , bool> ReturnPair;表示,这一楼梯带作为第一值和第二行的ool。

Question:
How to insert bool here: rollCallRegister.insert (pair <int, string> (1, "anisha"));?

第二,pair <map <int, string> 这一表将地图的炉.作为初步投入。

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

最佳回答

<代码>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).

问题回答

你还可以在<滚动>上插入一个<<>bool>/strong>,因为它只作为关键内容,而且用你目前的形式显示其价值。

如果您希望rollCallRegistermap,将pair of (int, string)作为主要bool as Value 你们需要改写:

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

公平三维:pair<pair <int, string> bool>。 您不妨查看<代码>make_pair。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签