How to write a 3d mapping in C ++?

Could you tell me how I can write a multidimensional map. For two-dimensional display, I did the following:

map<string, int> Employees
Employees["person1"] = 200;

I tried to use something like the following for 3d matching.

map<string, string, int> Employees;
Employees["person1", "age"] = 200;

Could you tell me the right way to do this?

and is there a way to initialize all map elements to 0? As in the array, we can sayint array[10]={0};

+5
source share
4 answers

You need to create a map of such cards.

map<string, map<string, int> > employees;
employees["person1"]["age"] = 200;
+9
source

You can use the pairlibrary class utilityto combine two objects:

map<pair<string, string>, int> Employees;
Employees[make_pair("person1", "age")] = 200;

See http://www.cplusplus.com/reference/std/utility/pair/

+6
source

Instead of a nested map, you can use a tuple as keys; (this is C ++ 11 code, you can do the same with boost::tuple).

#include<map>
#include<tuple>
#include<string>
using namespace std;
map<tuple<string,string>,int> m;
int main(){
    m[make_tuple("person1","age")]=33;
}
+3
source

what you are doing here is not a 3D mapping, but a 2D mapping, how to use stl :: map as two dimensional arrays

The correct three-dimensional display will look like

map<string, map<string, map<int, string> > > Employees;
Employees["person1"]["age"][20] = "26/10/2014";
-1
source

All Articles