Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert pair into map

Tags:

c++

insert

map

I have the following map structure: map < pair < int,int >, object* > and I wish to insert into it.

How would I do it since I am trying to insert a pair and an object and I must make a pair out of this?

Should I create a new pair using make_pair() out of the pair and object that I have? If so, could you please let me know how to do this?

like image 849
Myx Avatar asked Feb 22 '10 15:02

Myx


People also ask

How do you add a pair to a map?

int a=10,b=20; map < pair < int,int >, int > m; pair < int,int >numbers = make_pair(a,b); int sum=a+b; m[numbers]=sum; Our map will have its key as pairs of numbers. We can access the integer values of pair variable using dot(.) operator.

Can we use pair as a key in map?

Do you mean cout << mymap[make_pair(1,2)] << endl; ? (1,2) is non-sensical, at least in this context. You must have an std::pair to be used as your key, and that means following what @andre just commented. Yes!

How do you use pairs as values on a map?

The std::map operator[] returns a reference to the map element identified by 100 (key), which is then overwritten by the pair returned by std::make_pair(10,10). I would suggest: map. insert( std::make_pair( 100, std::make_pair(10,10) ) );

How do you add a key-value to a map?

put() method of HashMap is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping to into a particular map. If an existing key is passed then the previous value gets replaced by the new value. If a new pair is passed, then the pair gets inserted as a whole.


2 Answers

object * myObject = // get an object somehow
myMap.insert(std::make_pair(std::make_pair(1,2), myObject));

or

typedef map<pair<int, int>, object *> MapType;
object * myObject = // get an object somehow
myMap.insert(MapType::value_type(std::make_pair(1,2), myObject));
like image 84
Nick Meyer Avatar answered Sep 26 '22 02:09

Nick Meyer


Assuming you're using C++11 or later, the best approach is probably:

object * myObject = // get an object somehow
myMap.emplace({1,2}, myObject);

For maps, emplace can be thought of as a version of insert that takes the key and value as separate arguments (it can actually take any combination of arguments that the corresponding pair type's constructors can take). In addition to being syntactically cleaner, it's also potentially more efficient than make_pair, because make_pair will usually produce an output whose type doesn't precisely match the value_type of the container, and so it incurs an unnecessary type conversion.

I used to recommend this, which also only works in C++11 or later:

object * myObject = // get an object somehow
myMap.insert({{1,2}, myObject});

This avoids the slightly surprising use of emplace, but it formerly didn't work if the key or value type are move-only (e.g. unique_ptr). That's been fixed in the standard, but your standard library implementation may not have picked up the fix yet. This might also theoretically be slightly less efficient, but in a way that any halfway decent compiler can easily optimize away.

like image 44
Geoff Romer Avatar answered Sep 24 '22 02:09

Geoff Romer