Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting into nested map in C++

Tags:

c++

Assume I have a nested map of type pointer. Then is there a single line statement to insert into the nested map,

map<int, map<int, int> >* nestedMap;

Currently I am doing this in 2 steps. First creating innermap and then insert into outer map as below,

nestedMap->insert(make_pair(int, map<int, int>)(int, innermap));

If the map is not pointer type, then i can insert easily like this,

nestedMap[int][int] = int;

Is there any simple ways for inserting into nested map of type pointer ?

thanks Prabu

like image 351
Prabu Avatar asked Dec 16 '25 22:12

Prabu


2 Answers

map::operator[] automatically creates the key/value pair if it doesn't exist.
(That's why it's not const!) So you don't need to create the inner map manually.

If you want to avoid creating the pair automatically, then use map::find() or map::at().

like image 77
user541686 Avatar answered Dec 19 '25 13:12

user541686


I believe the simplest one-liner is:

(*nestedMap)[int][int] = int;
like image 33
Angew is no longer proud of SO Avatar answered Dec 19 '25 15:12

Angew is no longer proud of SO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!