Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert pair as map value

Tags:

c++

map

stl

typedef pair<unsigned char, unsigned char> pair_k;
map<unsigned char, pair_k> mapping;

Which will be used this way:

mapping[100] = make_pair(10,10);

Question is:

  1. Is this allowed? Syntaxically, it feels alright.
  2. Would this be access as an array as oppose to a map?
like image 602
freonix Avatar asked May 13 '11 11:05

freonix


People also ask

Can I use pair as key in map C++?

So, you can use pair as a key in a map as follows: map<pair<int,string> , long> mp; mp. insert(make_pair(make_pair(2,"me"),123456789);

Are maps and pairs same?

A pair is a single unit with two members. A map has keys and values in it. So you can use pairs to fill up a map, the elements of the pair becoming key and value.

Can we put pair in map?

We generally see map being used for standard data types. We can also use map for pairs.

What is the difference between pair and map in C++?

std:pair holds exactly two objects. std:map hold a collection of paired objects.


1 Answers

That looks ok to me. But note that this is not array access; it just looks like it because std::map overloads operator[]. If you do mapping.size() afterwards, you will find that it will be 1.

like image 65
Oliver Charlesworth Avatar answered Nov 07 '22 20:11

Oliver Charlesworth