在C实现Pythonesque为 '字典':
back={{(0,2,"ADJ"):("NP+V","ADJ",0)},{(0,2,"U"):("NP",".",1)}}
你会需要以下两种结构类型(一个用于左手键,一个对于右边的值),或者你将使用字符串的一切。
使用你的代码作为一个松散的基础上,你可能有:
struct ap
{
int i1;
int i2;
char s1[20];
};
struct bp
{
char s1[20];
char s2[20];
int i1;
};
,也许:
struct Dictionary
{
struct ap key;
struct bp value;
};
然后,您可以使用一个初始化:
struct Dictionary oed[] =
{
{ { 0, 2, "ADJ" }, { "NP+V", "ADJ", 0 } },
{ { 0, 2, "U" }, { "NP", ".", 1 } },
};
如果您需要要创建并填充结构,可以使用C99复合文字:
struct Dictionary chambers = (struct Dictionary){ { 0, 2, "ADJ" }, { "NP+V", "ADJ", 0 } };
还是做任务零碎:
struct Dictionary merriam;
merriam.key = (struct ap){ 0, 2, "ADJ" };
merriam.value = (struct bp){ "NP+V", "ADJ", 0 };
或做任务的成员在同一时间:
struct Dictionary webster;
webster.key.i1 = 0;
webster.key.i2 = 2
strcpy(webster.key.s1, "ADJ");
strcpy(webster.value.s1, "NP+V");
strcpy(webster.value.s2, "ADJ");
webster.value.i1 = 0;
注意,这个轮廓简单地存储数据的交易。它不涉及对关键值或其他花哨的(必要的)内存管理进行散列处理。该代码假定字符串将适合;它应该检查字符串是否符合。
但是,没有用于管理这种结构的标准C库;你要么写自己的,要么借别人的图书馆。
'c'中的字典? O.o –
C没有内置字典类型。你必须使用第三方库。 – dandan78