0

I am new to map in C++ and have one 3D map, like this

map<int,map<const char*,const char*>>Map3D;

I want to insert values in such a way that int value increments when both const char* values are more than 50. I am keeping a track of const char* value. Can anyone tell me how i can insert value in this map. I am doing something like this

Map3D.insert(pair<int,map<const char* ,const char*>(count,pair<const char*,const char*>(TempA,TempB)));

But it's not working.

EDIT

std::map<int,map<const char*,const char*>>::iterator it= Map3D.begin();
std::map<const char*, const char*>::iterator sub_it = subMap.begin();
Hamid
  • 137
  • 1
  • 11
  • Why are you using a `map`? That will not sort the elements lexicographically. Lookup will be difficult too. Consider using a `map` instead. – D Drmmr Jun 23 '14 at 09:04
  • @DDrmmr, I am filling this map with a output from a API function and that function return `const char*` – Hamid Jun 23 '14 at 09:54
  • And how do you know that that function always returns the same pointer for the same string value? Or different pointers for different string values for that matter? – D Drmmr Jun 23 '14 at 10:06
  • there is one iterator in the function, which iterates through the object returned by the function. I am tracking the iterator. – Hamid Jun 23 '14 at 10:09

1 Answers1

1

You have a missing >, and you can't recursively insert std::pair into inner maps, you'll have to either create a new map or use an existing one

  map<int,map<const char*,const char*>> Map3D;
  map<const char*,const char*> subMap;

  const char hello[] = "hello";
  const char world[] = "world";

  subMap.insert(std::pair<const char*,const char*>(hello, world));
  Map3D.insert(std::pair<int,map<const char*,const char*>>(22, subMap));

Edit: to read back elements take a look at http://www.cplusplus.com/reference/map/map/operator[]/

  map<const char*,const char*> subMapCopy = Map3D[22];
  cout << subMapCopy[hello]; // world

Edit II: with iterators: http://www.cplusplus.com/reference/map/map/begin/

 std::map<int,map<const char*,const char*>>::iterator it= Map3D.begin();
 std::map<const char*, const char*>::iterator sub_it = subMap.begin();
 cout << it->second[sub_it->first]; // world
Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • Sir, I want to access the elements by iterator method. I made two iterators and edited the question. If not too pressing for you, can you please help with them. Thanks – Hamid Jun 23 '14 at 09:52
  • 1
    @Hamid Edited II, but this is getting off-topic, make another question if you add other data – Marco A. Jun 23 '14 at 10:14
  • Please have a look at it..http://stackoverflow.com/questions/24366401/inserting-and-reading-values-from-3d-map – Hamid Jun 23 '14 at 12:51