View Single Post
  #9  
Old 07-06-2009, 02:44 AM
KLS
Administrator
 
Join Date: Sep 2006
Posts: 1,348
Default

I wrote up a simple example that should be fairly informative with regards to nesting one map in another. Keep in mind something the >> on the end of the map decl doesn't compile on g++, which is why it's > >, gnu translates it as an operator instead of apart of the declaration. At least last time I tried it this was the case.

Code:
#include <stdio.h>
#include <iostream>
#include <string>
#include <map>

int main()
{
	std::map<int, std::map<int, std::string> > some_double_map;

	std::map<int, std::string> string_map;
	string_map[1] = "Wow";
	string_map[9] = "Bonk";

	std::map<int, std::string> string_map_two;
	string_map_two[3] = "Rich";
	string_map_two[1] = "Test";

	some_double_map[0] = string_map;
	some_double_map[1] = string_map_two;

	std::map<int, std::map<int, std::string> >::iterator iter = some_double_map.find(0);

	if(iter != some_double_map.end())
	{
		std::map<int, std::string>::iterator iter_inner = iter->second.find(1);
		if(iter_inner != iter->second.end())
		{
			printf("Found string %s\n", iter_inner->second.c_str());
		}
		else
		{
			printf("Failed to find string =(\n");
		}
	}
	else
	{
		printf("Failed to find std::map =(\n");
	}

	iter = some_double_map.find(1);
	if(iter != some_double_map.end())
	{
		std::map<int, std::string>::iterator iter_inner = iter->second.find(1);
		if(iter_inner != iter->second.end())
		{
			printf("Found string %s\n", iter_inner->second.c_str());
		}
		else
		{
			printf("Failed to find string =(\n");
		}
	}
	else
	{
		printf("Failed to find std::map =(\n");
	}

	std::cin.get();
	return 0;
}
Reply With Quote