在C中映射类似结构的结构:使用int和struct确定一个值 [英] Map like structure in C: use int and struct to determine a value

查看:57
本文介绍了在C中映射类似结构的结构:使用int和struct确定一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经用C ++编写代码,现在我尝试使用C进行编程.

I used to code in C++ and now I try to program in C.

假设我已经定义了一个结构

Assume I have defined a struct

struct point{
    int x;
    int y;
}

c中是否有任何数据结构A可以支持以下功能: 给定两个整数,例如ij,以及两个点,例如p1p2. A[i][j][p1][p2]可以唯一地确定一个值.

Is there any data structure A in c that can support the following functionality: Given two integers, say i and j, and two points, say p1 and p2. A[i][j][p1][p2] can uniquely determine a value.

听起来像是4维数组.但是,索引不再是整数,而是用户定义的struct.

It sounds like a 4-d array. However, indexes are no longer a int, but user-defined struct.

推荐答案

您可能必须构建自己的结构. Kernighan和Ritchie编写的C编程语言给出了一个用c制作关联映射的示例,下面我将基于我从中所记起的内容进行详细说明.

You'll probably have to make your own structure. The C Programming Language by Kernighan and Ritchie has an example of making an associate map in c, and what I'll detail below is based on what I remember from that.

基本上,您将需要一个结构 Map ,其中包含结构 Key 和结构 Value .

Basically you'll need a struct Map that contains struct Key and struct Value.

struct Map {
    struct Key key;
    struct Value value;
};

结构包含确定值的元素(在您的情况下为2点和2个整数)

struct Key contains elements that determine the value (in your case 2 points and 2 ints)

struct Key {
    struct point p1;
    struct point p2;
    int i;
    int j;
};

结构是您希望键指向的内容(您未说)

struct Value is whatever you want your key to point to (you didn't say)

您现在有了一个结构 Map ,该结构将您的四个输入与一个值相关联,但是单个映射并没有那么有用.您将需要它们的整个阵列.

You now have a struct Map that 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 map[SIZE_OF_MAP];

如果您不想在数组中线性搜索所需的 Map 结构,则可以创建一个散列函数,将其直接带到该结构.只需定义一个使用键并使用其值在数组中为其分配索引的函数即可.使用哈希将地图放置在数组中,然后从数组中检索它. (注意:我不确定这是否是正确的哈希示例,如果完全错误,请更正)

If you don't want to linearly search the array for the Map struct 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 Map in 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)

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);
}

如果使用哈希函数,则必须考虑冲突(当两个键为 get_hash 提供相同结果时会发生什么).使用地图数组时,您需要某种形式的冲突解决方案.

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.

这篇关于在C中映射类似结构的结构:使用int和struct确定一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆