如何使用stl :: map作为二维数组 [英] how to use stl::map as two dimension array
本文介绍了如何使用stl :: map作为二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您能告诉我们如何使用stl:map作为二维数组吗?我想访问像mymap [i] [j]这样的个别元素,我不知道i或j的值是什么。
Could you let us know how to use stl:map as two dimension array? I wanted to access the individual elements as like mymap[i][j] where I do not know beforehand what the value of i or j could be. Any better ideas to do the same thing in other way?
推荐答案
你可以做
std::map<int, std::map<int, int> > mymap;
例如:
#include <map>
#include <iostream>
int main()
{
std::map<int, std::map<int, int> > mymap;
mymap[9][2] = 7;
std::cout << mymap[9][2] << std::endl;
if (mymap.find(9) != mymap.end() && mymap[9].find(2) != mymap[9].end()) {
std::cout << "My map contains a value for [9][2]" << std::endl;
} else {
std::cout << "My map does not contain a value for [9][2]" << std::endl;
}
return 0;
}
在标准输出中打印7,其后是我的地图包含[9] [2]。
prints 7 on the standard output, followed by "My map contains a value for [9][2]".
这篇关于如何使用stl :: map作为二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文