English 中文(简体)
是否可能在C中 im树 t子?
原标题:Is it possible to imitate python tuples in C?
  • 时间:2010-06-13 20:27:14
  •  标签:
  • c
  • tuples

I am just curious to know what are the strategies ? (If the strategies exists).

最佳回答

C中一种典型模式是使用< 代码>struct。 包含一个内容,即可以储存的基本种类的<编码>union>,而一个内容是enum,其中哪些类型的union正在用于本例。

然后,您可使用一系列此类物体,或许可作为<条码>truct。 载有一些物品,还有这些物品的储存点。

例如:

#include <stdio.h>
#include <stdlib.h>

typedef enum tuple_datatype_e {
    TUPLE_DATATYPE_INT,
    TUPLE_DATATYPE_FLOAT,
    TUPLE_DATATYPE_STRING
} tuple_datatype_t;

typedef struct tuple_item_s {
    tuple_datatype_t datatype;
    union {
        int   int_val;
        float float_val;
        char *string_val;
    } u;
} tuple_item_t;

typedef struct tuple_s {
    unsigned int n_items;
    tuple_item_t *items;
} tuple_t;

static void print_tuple(tuple_t *t)
{
    unsigned int i;

    printf("(");
    for (i = 0; i < t->n_items; i++) {
        if (i > 0)
            printf(", ");
        switch (t->items[i].datatype) {
        case TUPLE_DATATYPE_INT:
            printf("%d", t->items[i].u.int_val);
            break;
        case TUPLE_DATATYPE_FLOAT:
            printf("%f", t->items[i].u.float_val);
            break;
        case TUPLE_DATATYPE_STRING:
            printf(""%s"", t->items[i].u.string_val);
            break;
        }
    }
    printf(")
");
}

int main(void)
{
    tuple_t foo;

    foo.n_items = 3;
    foo.items = malloc(sizeof(tuple_item_t) * foo.n_items);
    foo.items[0].datatype = TUPLE_DATATYPE_INT;
    foo.items[0].u.int_val = 123;
    foo.items[1].datatype = TUPLE_DATATYPE_FLOAT;
    foo.items[1].u.float_val = 4.56;
    foo.items[2].datatype = TUPLE_DATATYPE_STRING;
    foo.items[2].u.string_val = "789";
    print_tuple(&foo);

    return 0;
}
问题回答

在C区,最接近于甲型 t,可能取决于你如何使用这些.子或阵列。

如果你想要将可能不同类型的固定数量的相关价值分类,则使用结构。

如果你想要某种同类的价值观,并且能够将各种数值编入阵列,则使用阵列。

尽管并非完全相同,但组合体至少有一些相同特性。 如果你需要更精确的效仿,东帝汶国防军可以做trick。

C区有有APICTuples,有APICAP

http://linuxtuples.sourceforge.net/





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...