C语言 C 中的类似映射的结构:使用 int 和 struct 来确定一个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21958247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Map like structure in C: use int and struct to determine a value
提问by user3341338
I used to code in C++ and now I try to program in C.
我曾经用 C++ 编码,现在我尝试用 C 编程。
Assume I have defined a struct
假设我已经定义了一个结构
struct point{
int x;
int y;
}
Is there any data structure Ain cthat can support the following functionality:
Given two integers, say iand j, and two points, say p1and p2.
A[i][j][p1][p2]can uniquely determine a value.
是否有任何数据结构A中c,可支持以下功能:给定两个整数,说i和j,和两个点,说p1和p2。
A[i][j][p1][p2]可以唯一确定一个值。
It sounds like a 4-d array. However, indexes are no longer a int, but user-defined struct.
这听起来像一个 4 维数组。但是,索引不再是 int,而是用户定义的struct。
回答by gslavin
You'll probably have to make your own structure. The C Programming Language by Kernighan and Ritchiehas an example of making an associate map in c, and what I'll detail below is based on what I remember from that.
您可能必须制作自己的结构。 Kernighan 和 Ritchie 的 C Programming Language有一个在 c 中制作关联映射的示例,我将在下面详细介绍的内容基于我从中记住的内容。
Basically you'll need a struct Mapthat contains struct Keyand struct Value.
基本上你需要一个包含 struct Key和 struct Value的 struct Map。
struct Map {
struct Key key;
struct Value value;
};
struct Keycontains elements that determine the value (in your case 2 points and 2 ints)
struct Key包含确定值的元素(在您的情况下为 2 个点和 2 个整数)
struct Key {
struct point p1;
struct point p2;
int i;
int j;
};
struct Valueis whatever you want your key to point to (you didn't say)
struct Value是您希望密钥指向的任何内容(您没有说)
You now have a struct Mapthat associates your four inputs with a value, but a single map isn't that useful. You're going to want a whole array of them.
您现在有一个 struct Map将您的四个输入与一个值相关联,但单个映射并没有那么有用。你会想要一整套它们。
struct Map map[SIZE_OF_MAP];
If you don't want to linearly search the array for the Mapstruct you're looking for, you can make a hashing function that will bring you directly to it. Just define a function that takes the key and uses its value to assign it an index in the array. Use the hash to place the Mapin the array and retrieve it from the array. (Note: I'm unsure if this is a correct example of hashing, please correct if this is completely wrong)
如果您不想线性搜索数组以查找您要查找的Map结构,您可以创建一个散列函数,将您直接带到它。只需定义一个函数,该函数接受键并使用其值在数组中为其分配一个索引。使用散列将Map放入数组并从数组中检索它。(注意:我不确定这是否是一个正确的散列示例,如果这是完全错误的,请更正)
int get_hash(Key *key)
{
int result;
/* combine all inputs in some way */
result = key->i * key->i + (key->p1.x * key->p1.x) - (key->p2.x * key->p2.x)
/* make sure result isn't out of bounds of the array */
return (result % SIZE_OF_MAP);
}
If you use the hashing function you'll have to consider collisions (what happens when two keys give the same result for get_hash). When you use your array of Maps you'll need some form of collision resolution.
如果您使用散列函数,则必须考虑冲突(当两个键为get_hash提供相同结果时会发生什么)。当您使用 Map 数组时,您将需要某种形式的碰撞解决方案。

