C语言 在 C 中创建元组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22727404/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:56:09  来源:igfitidea点击:

Making a Tuple in C

ctuplesbindassociative-arraykey-value

提问by user2757849

I have a problem right now where I'm trying to essentially bind a string to a value. What would be the easiest way to associate these values in C? I essentially want a Python like tuple that I can add to a list.

我现在有一个问题,我试图将一个字符串绑定到一个值。在 C 中关联这些值的最简单方法是什么?我本质上想要一个类似 Python 的元组,我可以将其添加到列表中。

回答by paxdiablo

That's what C structures are for. For example, the following structure is a tuple which binds a string (up to twenty characters long) to an integer value:

这就是 C 结构的用途。例如,以下结构是一个元组,它将字符串(最多 20 个字符长)绑定到一个整数值:

typedef struct {
    char strVal[21];
    int intVal;
} tTuple;

Variables of type tTuple(or pointers to them if they're dynamically allocated) can be passed around, added to lists, and manipulated in any way that makes sense to your situation.

类型变量tTuple(或指向它们的指针,如果它们是动态分配的)可以传递、添加到列表中,并以任何对您的情况有意义的方式进行操作。



Using a structure similar to the one above, the following complete program shows this in action. It could probably do with some more sanity checks(a)but should be fine for just showing how to do a tuple (which is what the question was asking about):

使用与上述结构类似的结构,以下完整程序显示了这一点。它可能可以做一些更多的健全性检查(a),但只展示如何做一个元组(这就是问题所问的)应该没问题:

#include <stdio.h>
#include <string.h>

static struct { char strVal[21]; int intVal; } tuple[10];
static int tupleCount = 0;

static void listTuples(void) {
    printf("==========\nTuple count is %d\n", tupleCount);
    for (int i = 0; i < tupleCount; ++i)
        printf("   [%s] -> %d\n", tuple[i].strVal, tuple[i].intVal);
    puts("==========");
}

static void addTuple(char *str, int val) {
    printf("Adding '%s', mapped to %d\n", str, val);
    strcpy(tuple[tupleCount].strVal, str);
    tuple[tupleCount++].intVal = val;
}

static void deleteTuple(char *str) {
    int index = 0;
    while (index < tupleCount) {
        if (strcmp(str, tuple[index].strVal) == 0) break;
        ++index;
    }
    if (index == tupleCount) return;

    printf("Deleting '%s', mapped to %d\n", str, tuple[index].intVal);
    if (index != tupleCount - 1) {
        strcpy(tuple[index].strVal, tuple[tupleCount - 1].strVal);
        tuple[index].intVal = tuple[tupleCount - 1].intVal;
    }
    --tupleCount;
}

int main(void) {
    listTuples();

    addTuple("aardvark", 31);
    addTuple("buffalo", 41);
    addTuple("camel", 59);
    addTuple("dingo", 27);
    listTuples();

    deleteTuple("buffalo");
    listTuples();

    return 0;
}

The outputof that program is:

该程序的输出是:

==========
Tuple count is 0
==========
Adding 'aardvark', mapped to 31
Adding 'buffalo', mapped to 41
Adding 'camel', mapped to 59
Adding 'dingo', mapped to 27
==========
Tuple count is 4
   [aardvark] -> 31
   [buffalo] -> 41
   [camel] -> 59
   [dingo] -> 27
==========
Deleting 'buffalo', mapped to 41
==========
Tuple count is 3
   [aardvark] -> 31
   [dingo] -> 27
   [camel] -> 59
==========


(a)Such as checking the string length and array count for overflow, or disallowing duplicate keys (if desired).

(a)例如检查字符串长度和数组计数是否溢出,或禁止重复键(如果需要)。

回答by luser droog

An alternate way to associate values of different types is to create parallel arrays. One array for each typed element, associated by having the same index.

关联不同类型值的另一种方法是创建并行数组。每个类型元素的一个数组,通过具有相同的索引关联。

char *strVal[5];
int intVal[5];

So strVal[0]is associated with intVal[0], and so on.

SostrVal[0]与 相关联intVal[0],依此类推。

This way of representing a tuple can be applied even in a language without a structor recordtype.

即使在没有 astructrecord类型的语言中也可以应用这种表示元组的方式。