Using pair as key in a map (C++ / STL) -


I want to use a pair as STT key as a map key.

  Add # & lt; Iostream & gt; # Include & lt; Map & gt; using namespace std; Int main () {typedef pair & lt; Char *, int & gt; Key; Typedef Map & lt; Key, Four * & gt; Mapa; Key Page 1 ("Apple", 45); Key Page 2 ("Berry", 20); Mapa mapa; Mapa.insert (p1, "manzana"); Mapa.insert (P2, "Arndano"); Return 0; }  

But the compiler throws a bunch of unreadable information and I am very new to C and C ++.

How can I use a pair as a key? Map? And in general what kind of structure (objects, strokes, etc.) can I use as a key in the map?

Thank you!

std :: map :: insert takes a single argument: the key Value added so that you will need to use it:

  mapa.insert (std :: make_pair (p1, "manzana"));  

You should use std :: string instead of the C string in your types. As is now, you will not get results that are likely to result, because values ​​are being searched in the map, not by comparing signs, by comparing signs, by comparing stars.

If you really want to use the C strings (which, again, you should not do), then you will be able to type in char * instead of char * in your types Need to use char * .

And how can I use any kind of structure (objects, strokes, etc.) as a key in the map?

Use the key type or custom comparator for the overload operator .


Comments