我有一个 std::map ,用于存储 x 和 y 坐标的值。 我的数据非常稀疏,所以我不想使用数组或向量,这会导致内存的大量浪费。 我的数据范围从-250000到250000,但我最多只有几千个点。
目前,我正在使用两个坐标(即“12x45”)创建一个 std::string 并将其用作键。 这似乎不是最好的方法。
我的其他想法是使用 int64 并将两个 int32 推入其中并将其用作密钥。
或者使用具有两个坐标的类。 用作密钥的类有哪些要求?
做这个的最好方式是什么? 我宁愿不使用地图的地图。
I have a std::map
that I'm using to store values for x and y coordinates. My data is very sparse, so I don't want to use arrays or vectors, which would result in a massive waste of memory. My data ranges from -250000 to 250000, but I'll only have a few thousand points at the most.
Currently I'm creating a std::string
with the two coordinates (i.e. "12x45"
) and using it as a key. This doesn't seem like the best way to do it.
My other thoughts were to use an int64 and shove the two int32s into it and use it as a key.
Or to use a class with the two coordinates. What are the requirements on a class that is to be used as the key?
What is the best way to do this? I'd rather not use a map of maps.
发布评论
评论(10)
使用 std::pair 对于关键:
Use std::pair<int32,int32> for the key:
我通常这样解决这类问题:
I usually solve this kind of problem like this:
Boost 有一个使用一个或多个索引的映射容器。
多索引映射
Boost has a map container that uses one or more indices.
Multi Index Map
映射需要能够判断一个键的值是否小于另一个键的值:默认情况下,这意味着 (key1 < key2) 必须是有效的布尔表达式,即键类型应实现“小于”运算符。
映射模板还实现了一个重载构造函数,它允许您传入对 key_compare 类型的函数对象的引用,该函数对象可以实现比较运算符:这样,比较就可以作为此外部函数对象的方法来实现,而不是需要将其融入到您的密钥的任何类型中。
The map needs to be able to tell whether one key's value is less than another key's value: by default this means that (key1 < key2) must be a valid boolean expression, i.e. that the key type should implement the 'less than' operator.
The map template also implements an overloaded constructor which lets you pass-in a reference to a function object of type key_compare, which can implement the comparison operator: so that alternatively the comparison can be implemented as a method of this external function object, instead of needing to be baked in to whatever type your key is of.
这会将多个整数键填充到一个大整数中,在本例中为 _int64。 它与 _int64 进行比较,AKA long long (有史以来最丑陋的类型声明。short Short Short Short,只会稍微不那么优雅。10 年前它被称为 vlong。好多了。“进步”就这么多),所以没有比较需要功能。
提供了这个答案后,我怀疑这是否适合您,因为您需要两个单独且不同的键来在二维(X 和 Y)中导航。
另一方面,如果您已经有了 XY 坐标,并且只想将一个值与该键关联起来,那么这会非常有效,因为 _int64 比较与 Intel X86 芯片上的任何其他整数比较所需的时间相同 - 1 个时钟。
在这种情况下,该合成密钥的比较速度是三重复合密钥的 3 倍。
如果使用它来创建一个稀疏的电子表格,我将使用两棵不同的树进行 RX,一棵树嵌套在另一棵树中。 让 Y 维度成为“老板”,并首先在 Y 空间中搜索分辨率,然后再进行 X 维度。 电子表格的高度大于宽度,并且您始终希望任何复合键中的第一个维度具有最大数量的唯一值。
这种安排将为 Y 维度创建一个地图,该地图将具有 X 维度的地图作为其数据。 当您到达 Y 维度中的叶子时,您开始在其 X 维度中搜索电子表格中的列。
如果您想创建一个非常强大的电子表格系统,请以相同的方式添加 Z 维度,并将其用作组织单位的示例。 这是一个非常强大的预算/预测/会计系统的基础,该系统允许管理单位拥有大量详细帐户来跟踪管理费用等,并且不会让这些帐户占用具有自己类型的行单位的空间要跟踪的细节。
This will stuff multiple integer keys into a large integer, in this case, an _int64. It compares as an _int64, AKA long long (The ugliest type declaration ever. short short short short, would only be slightly less elegant. 10 years ago it was called vlong. Much better. So much for "progress"), so no comparison function is needed.
Having provided this answer, I doubt this is going to work for you, as you need two separate and distinct keys to navigate with in 2 dimensions, X and Y.
On the other hand, if you already have the XY coordinate, and just want to associate a value with that key, then this works spectacularly, because an _int64 compare takes the same time as any other integer compare on Intel X86 chips - 1 clock.
In this case, the compare is 3X as fast on this synthetic key, vs a triple compound key.
If using this to create a sparsely populated spreadsheet, I would RX using 2 distinct trees, one nested inside the other. Make the Y dimension "the boss", and search Y space first to resolution before proceeding to the X dimension. Spreadsheets are taller than they are wide, and you always want the 1st dimension in any compound key to have the largest number of unique values.
This arrangement would create a map for the Y dimension that would have a map for the X dimension as it's data. When you get to a leaf in the Y dimension, you start searching it's X dimension for the column in the spreadsheet.
If you want to create a very powerful spreadsheet system, add a Z dimension in the same way, and use that for, as an example, organizational units. This is the basis for a very powerful budgeting/forecasting/accounting system, one which allows admin units to have lots of gory detail accounts to track admin expenses and such, and not have those accounts take up space for line units which have their own kinds of detail to track.
我认为对于您的用例,
std::pair
,正如David Norman的回答中所建议的,是最好的解决方案。 但是,从 C++11 开始,您也可以使用std::tuple
。 如果您有两个以上的键,例如,如果您有 3D 坐标(即x
、y
和z
),则元组非常有用。 那么你就不必嵌套对或为结构
定义比较器。 但对于您的特定用例,代码可以编写如下:输出:
注意:自C++17开始工作使用元组变得更加容易,特别是如果您想同时访问多个元素。
例如,如果您使用 结构化绑定,则可以按如下方式打印元组:
Coliru 上的代码
I think for your use case,
std::pair
, as suggested in David Norman's answer, is the best solution. However, since C++11 you can also usestd::tuple
. Tuples are useful if you have more than two keys, for example if you have 3D coordinates (i.e.x
,y
, andz
). Then you don't have to nest pairs or define a comparator for astruct
. But for your specific use case, the code could be written as follows:Output:
Note: Since C++17 working with tuples has become easier, espcially if you want to access multiple elements simultaneously.
For example, if you use structured binding, you can print the tuple as follows:
Code on Coliru
使用 std::pair。 如果您有许多此类映射,甚至最好使用 QHash,int> 。
Use std::pair. Better even use
QHash<QPair<int,int>,int>
if you have many of such mappings.希望您会发现它很有用:
Hope you will find it useful:
顶部结果的替代方案,性能稍差,但可以更轻松地建立索引
An alternative for the top result that is slightly less performant but allows for easier indexing
首先也是最重要的,放弃字符串并使用 2 个整数,您现在可能已经完成了。 感谢您发现树是实现稀疏矩阵的最佳方法。 看起来通常会吸引不良实施。
仅供参考,三重复合键也可以使用,我假设也是一对。
虽然它会产生一些丑陋的子脚本,所以一点宏魔法会让你的生活更轻松。 我留下了这个通用目的,但如果您为特定映射创建宏,则在宏中对参数进行类型转换是一个好主意。
TresKey12
经过测试并且运行良好。QuadKeys
也应该可以工作。注意:只要您的关键部分是基本数据类型,您就不需要再编写任何内容。 AKA,无需担心比较功能。 STL 可以满足您的需求。 只需将其编码并让它撕裂即可。
如果有人想给我留下深刻的印象,请向我展示如何为
TresKeys
创建一个不依赖嵌套对的比较运算符,这样我就可以使用具有 3 个成员的单个struct
并使用比较功能。PS: TresKey12 给我带来了关于声明为pair,z 的地图的问题,因为它会生成x,pair,而这两个玩起来不太好。 对于 DosKeys 或 QuadKeys 来说这不是问题。 如果周五是炎热的夏季,您可能会发现输入 DosEquis 会产生意想不到的副作用
...呃..DosKeys很多次,是对墨西哥啤酒的渴望。 买者自负。 正如谢尔登·库珀所说:“没有奇思妙想的生活还算什么?”。
First and foremost, ditch the string and use 2 ints, which you may well have done by now. Kudos for figuring out that a tree is the best way to implement a sparse matrix. Usually a magnet for bad implementations it seems.
FYI, a triple compound key works too, and I assume a pair of pairs as well.
It makes for some ugly sub-scripting though, so a little macro magic will make your life easier. I left this one general purpose, but type-casting the arguments in the macro is a good idea if you create macros for specific maps. The
TresKey12
is tested and running fine.QuadKeys
should also work.NOTE: As long as your key parts are basic data types you DON'T need to write anything more. AKA, no need to fret about comparison functions. The STL has you covered. Just code it up and let it rip.
If someone wants to impress me, show me how to make a compare operator for
TresKeys
that doesn't rely on nesting pairs so I can use a singlestruct
with 3 members and use a comparison function.PS: TresKey12 gave me problems with a map declared as pair,z as it makes x,pair, and those two don't play nice. Not a problem for DosKeys, or QuadKeys. If it's a hot summer Friday though, you may find an unexpected side-effect of typing in DosEquis
... err.. DosKeys a bunch of times, is a thirst for Mexican beer. Caveat Emptor. As Sheldon Cooper says, "What's life without whimsy?".