Can you please tell me how I can write multidimensional map. For two dimensional map, I did the following:
map<string, int> Employees
Employees[“person1”] = 200;
I was trying to use something similar to following for 3d mapping.
map<string, string, int> Employees;
Employees[“person1”, “age”] = 200;
Can you please tell me the correct way to do this?
and Is there a way I can initialize all the map elements to be 0 ? Just like on a array we can say int array[10]={0};
You need to create map of maps like that.
map<string, map<string, int> > employees;
employees["person1"]["age"] = 200;
You can use the pair
class of the utility
library to combine two objects:
map<pair<string, string>, int> Employees;
Employees[make_pair("person1", "age")] = 200;
See http://www.cplusplus.com/reference/std/utility/pair/
Instead of nested map, you can use tuple as keys; (this is a c++11 code, you could do the same with boost::tuple
as well).
#include<map>
#include<tuple>
#include<string>
using namespace std;
map<tuple<string,string>,int> m;
int main(){
m[make_tuple("person1","age")]=33;
}
what you are doing here is not 3D mapping but 2D mapping, how to use stl::map as two dimension array
Correct 3D mapping will be like
map<string, map<string, map<int, string> > > Employees;
Employees[“person1”][“age”][20] = "26/10/2014";