简体   繁体   English

C ++ std :: map vs dynamic array

[英]C++ std::map vs dynamic array

I'm trying to make a 3 dimensional array of booleans that tells me if I previously visited a location in 3d space for a simple navigation algorithm. 我正在尝试制作一个三维的布尔数组,它告诉我以前是否曾访问过3d空间中的某个位置以获得简单的导航算法。 The array could be quite large (something along the lines of 1,000,000 x 1,000,000 x 1,000,000 or maybe larger), so I'm wondering if it would be faster to declare an array of that size and set each boolean value to false, or to make a map with a key of coordinate (x, y, z) and a value of type bool. 这个数组可能非常大(类似于1,000,000 x 1,000,000 x 1,000,000或者更大的数字),所以我想知道声明这个大小的数组并将每个布尔值设置为false或者制作是否会更快带有坐标键(x,y,z)和bool类型值的地图。

From what I figure, the array would take O(1) to find or modify a coordinate, and the map would take O(log n) to find or insert a value. 根据我的图,数组将使用O(1)来查找或修改坐标,并且地图将采用O(log n)来查找或插入值。 Obviously, for accessing values, the array is faster. 显然,为了访问值,数组更快。 However, does this offset the time it takes to declare such an array? 但是,这是否会抵消声明此类数组所需的时间?

Thanks 谢谢

Even at 1 bit per bool, your array will take over 2**39 bytes. 即使每个bool为1位,您的阵列也将占用2 ** 39个字节。 I'd suggest a set if there aren't too many elements that will be true . 如果没有太多的元素是true ,我会建议一个set

You can use a class to hide the implementation details, and use a 1D set. 您可以使用类隐藏实现细节,并使用1D集。

Have you tried calculating how much memory would be needed for an array like this? 你有没有尝试计算像这样的阵列需要多少内存? A lot! 很多! Use std::map if ordering of the points is important, or std::unordeded_map if not. 如果点的排序很重要,请使用std :: map,否则使用std :: unordeded_map。 Also the unordered map gives you a constant time insertion and lookup. 此外,无序映射为您提供恒定的时间插入和查找。 I guess that some kind of search tree is probably what you're looking for (kd tree for example). 我想某些搜索树可能就是你要找的东西(例如kd树)。

You're going to make an array that is one exabyte, assuming that you use 8 bits per point? 假设您每点使用8位,那么您将创建一个exabyte的数组? Wow, you have a lot of RAM! 哇,你有很多内存!

I think you should re-think your approach. 我认为你应该重新思考你的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM