如何使用 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以执行
例如,
以下操作:在标准输出上打印 7,然后是“我的地图包含 [9][2] 的值”。
You can do
For example:
prints 7 on the standard output, followed by "My map contains a value for [9][2]".
Andrew Stein 的另一种解决方案与 STL 的其余部分配合得更好,就是简单地使用
例如,通过这种方式,您不需要链接两个对
map::find
的调用来搜索单个价值。An alternative solution to Andrew Stein's which plays nicer with the rest of STL is to simply use
For example, with this way you don't need to chain two calls to
map::find
to search for a single value.考虑使用 kd 树代替。每个级别的分支都会依次比较 i 和 j 的值。请参阅http://en.wikipedia.org/wiki/Kd-tree。
Consider using a kd-tree instead. Each level of branching will compare the i an j values in turn. See http://en.wikipedia.org/wiki/Kd-tree.