How to write 3d mapping in C++?

2019-04-28 04:27发布

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};

标签: c++ mapping
4条回答
Animai°情兽
2楼-- · 2019-04-28 05:11

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;
}
查看更多
Emotional °昔
3楼-- · 2019-04-28 05:20

You need to create map of maps like that.

map<string, map<string, int> > employees;
employees["person1"]["age"] = 200;
查看更多
Ridiculous、
4楼-- · 2019-04-28 05:30

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";
查看更多
太酷不给撩
5楼-- · 2019-04-28 05:32

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/

查看更多
登录 后发表回答