C中动态分配的关联数组的示例 [英] Example for an dynamically allocated associative array in C

查看:64
本文介绍了C中动态分配的关联数组的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直在攻击这个大约一个小时,所以我认为如果没有人会使用它,这种努力是没用的。


以下代码通常是一个黑客 - 例如用C语言实现一个关联数组,很多人在编写简单的C时似乎很想念。


我像往常一样试图遵循KISS原则。


我很想听到有关这方面的反馈,如果你能告诉我如何做得更好或者只是喜欢(或不喜欢)代码那就无所谓了。 />

许可证是DWTFYLWI,即用它你喜欢做什么。


这里有:


/ *

* tabletests - 在C中实现关联数组的几个函数
*

*版权所有2006 Felix H. Dahlke< fh*@textware.de>

*许可证:DWTFYLWI

* /


#include< stdio。 h>

#include< stdlib.h>

#include< string.h>


struct table_struct

{

char ** column1;

char ** column2;

int number_of_elements;

};


typedef struct table_struct table;


table *

create_table()/ *为新结构分配空间,它的初始元素为* /

{

table * tabl;


tabl = malloc(sizeof(tabl-> number_of_elements)+ sizeof(tabl-> column1)+ sizeof(tabl-> column2));


tabl-> column1 = malloc(sizeof(tabl-> column1));

tabl-> column2 = malloc(sizeof(tabl-> column2));

tabl-> number_of_elements = 0;


返回表格;

}


delete_table(tabl)/ *释放表格使用的大小* /

table * tabl;

{

int x;


if(!tabl)

return;


for(x = 0; x< tabl-> number_of_elements; x ++){

free(tabl-> column1 [x]);

free(tabl-> column2 [x]);

tabl-> column1 [x] = tabl-> column2 [x] = NULL;

}

free(tabl-> column1);

free(tabl-> column2);

free(tabl);


tabl-> column1 = tabl-> column2 = NULL;

tabl = NULL;

}


add_to_table(tabl,col1,col2)/ *添加元素(行)到桌子* /

table * tabl;

char * col1;

char * col2;

{

int num,s1,s2;


if(!tabl)

return;


/ * TODO:检查重复项?我们的目的并不真正需要* /


num = tabl-> number_of_elements ++;


tabl-> column1 = realloc(tabl- > column1,(num + 1)* sizeof(col1));

tabl-> column2 = realloc(tabl-> column2,(num + 1)* sizeof(col2));


s1 = strlen(col1);

s2 = strlen(col2);


tabl-> column1 [num] = malloc(s1 + 1);

tabl-> column2 [num] = malloc(s2 + 1);


strncpy( tabl-> column1 [num],col1,s1);

strncpy(tabl-> column2 [num],col2,s2);


tabl-> column1 [num] [s1] = tabl-> column2 [num] [s2] =''\ 0'';

}


char *

get_column(tabl,column,key)/ *返回其对应列与给定键匹配的元素* /

table * tabl;

短栏;

char * key;

{

char * value,* key2;

int x;


if(!tabl)

return(NULL);


for(x = 0; x< tabl-> number_of_elements; x ++){

switch(列){

案例1:{

key2 = tabl-> column1 [x];

value = tabl-> column2 [x];

休息;

}

案例2:{

key2 = tabl-> column2 [x];

value = tabl-> column1 [x];

break;

}

默认值:

返回(NULL);

}

if(!strcmp(key) ,key2))

返回(值);

}


返回(NULL);

}


print_table(tabl)/ *精美地打印表格* /

table * tabl;

{

int x;


if(!tabl)

return;


printf(" ; ====================================== \ n");

for(x = 0; x< tabl-> number_of_elements; x ++)

printf("%s |%s \ n ========== ============================ \ n",tabl-> column1 [x],tabl-> column2 [x] );

}


int main()

{

table * hobbies = create_table();


add_to_table(hobbies,play Tennis,Bob);

add_to_table(hobbies ,游泳,约翰);

add_to_table(爱好,写代码,费利克斯);

add_to_table(爱好,"看电视,丹尼尔);

add_to_table(爱好,油漆,珍妮);

print_table(爱好);

printf(Bob喜欢%s。\ n,get_column(爱好,2,Bob));


delete_table(爱好); < br $>
}


-

/〜\ the ascii ribbon campaign for html email

\\ \\和专有附件!

X

/ \永远不会将.doc .xls或.ppt文件附加到电子邮件中!

Have been hacking on this for about an hour, so I thought that this effort was useless if noone would use it.

The following code generally is a hack-example of the implementation of an associative array in C,
which many people apparently seem to miss when writing plain C.

I tried to follow the KISS principle as usual.

I''d love to hear feedback on this, doesn''t matter if you can tell me how to do it better or just like (or dislike) the code.

The license is DWTFYLWI, namely "do whatever the fuck you like with it".

Here it goes:

/*
* tabletests - several functions to implement associative arrays in C
*
* Copyright 2006 Felix H. Dahlke <fh*@textware.de>
* License: DWTFYLWI
*/

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

struct table_struct
{
char **column1;
char **column2;
int number_of_elements;
};

typedef struct table_struct table;

table *
create_table() /* allocates space for a new structure and it''s initial elements */
{
table *tabl;

tabl = malloc(sizeof(tabl->number_of_elements) + sizeof(tabl->column1) + sizeof(tabl->column2));

tabl->column1 = malloc(sizeof(tabl->column1));
tabl->column2 = malloc(sizeof(tabl->column2));
tabl->number_of_elements = 0;

return tabl;
}

delete_table(tabl) /* frees the size used by the table */
table *tabl;
{
int x;

if (!tabl)
return;

for (x = 0; x < tabl->number_of_elements; x++) {
free(tabl->column1[x]);
free(tabl->column2[x]);
tabl->column1[x] = tabl->column2[x] = NULL;
}
free(tabl->column1);
free(tabl->column2);
free(tabl);

tabl->column1 = tabl->column2 = NULL;
tabl = NULL;
}

add_to_table(tabl, col1, col2) /* adds elements (rows) to the table */
table *tabl;
char *col1;
char *col2;
{
int num, s1, s2;

if (!tabl)
return;

/* TODO: check for duplicates? not really needed for our purposes */

num = tabl->number_of_elements++;

tabl->column1 = realloc(tabl->column1, (num+1) * sizeof(col1));
tabl->column2 = realloc(tabl->column2, (num+1) * sizeof(col2));

s1 = strlen(col1);
s2 = strlen(col2);

tabl->column1[num] = malloc(s1+1);
tabl->column2[num] = malloc(s2+1);

strncpy(tabl->column1[num], col1, s1);
strncpy(tabl->column2[num], col2, s2);

tabl->column1[num][s1] = tabl->column2[num][s2] = ''\0'';
}

char *
get_column(tabl, column, key) /* returns the element whose corresponding column matches the given key */
table *tabl;
short column;
char *key;
{
char *value, *key2;
int x;

if (!tabl)
return(NULL);

for (x = 0; x < tabl->number_of_elements; x++) {
switch (column) {
case 1: {
key2 = tabl->column1[x];
value = tabl->column2[x];
break;
}
case 2: {
key2 = tabl->column2[x];
value = tabl->column1[x];
break;
}
default:
return(NULL);
}
if (!strcmp(key, key2))
return(value);
}

return(NULL);
}

print_table(tabl) /* prints the table beautifully */
table *tabl;
{
int x;

if (!tabl)
return;

printf("======================================\n") ;
for (x = 0; x < tabl->number_of_elements; x++)
printf("%s | %s\n======================================\n", tabl->column1[x], tabl->column2[x]);
}

int main()
{
table *hobbies = create_table();

add_to_table(hobbies, "play Tennis", "Bob");
add_to_table(hobbies, "swim", "John");
add_to_table(hobbies, "write code", "Felix");
add_to_table(hobbies, "watch TV", "Daniel");
add_to_table(hobbies, "paint", "Jenny");
print_table(hobbies);
printf("Bob likes to %s.\n", get_column(hobbies, 2, "Bob"));

delete_table(hobbies);
}

--
/~\ the ascii ribbon campaign against html email
\ / and proprietary attachments!
X
/ \ never attach .doc .xls or .ppt files to emails!

推荐答案

Felix H. Dahlke说:
Felix H. Dahlke said:
已经黑客攻击了大约一个小时,所以我想如果没人会使用它,这项努力是没用的。


如果我可以在没有闹铃的情况下一直读完它,我会

当然可以考虑使用它。
<以下代码通常是一个黑客 - 在C中实现一个
关联数组的例子,很多人在撰写简单的C时似乎很想念。

< snip>
tabl = malloc(sizeof(tabl-> number_of_elements)+ sizeof(tabl-> column1)+
sizeof(tabl-> column2));

tabl-> column1 = malloc(sizeof(tabl-> column1));
Have been hacking on this for about an hour, so I thought that this effort
was useless if noone would use it.
If I can read it all the way through without the alarm bells going off, I''ll
certainly consider using it.

The following code generally is a hack-example of the implementation of an
associative array in C, which many people apparently seem to miss when
writing plain C.
<snip>
tabl = malloc(sizeof(tabl->number_of_elements) + sizeof(tabl->column1) +
sizeof(tabl->column2));

tabl->column1 = malloc(sizeof(tabl->column1));




铃声!钟声!


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



The bells! The bells!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Hello Felix,
Hello Felix,
我很想听到有关这方面的反馈,如果你能告诉我
如何做得更好或者只是喜欢(或不喜欢)代码那就无所谓了。
I''d love to hear feedback on this, doesn''t matter if you can tell me
how to do it better or just like (or dislike) the code.




感谢您发布此内容(但是,请为其他人大喊将其发送给b
$ b)。

< politely>由于这一点,并且没有API工具来遍历

行或向下列,你动态分配的关联

数组提供的哈希表不会是什么提供?


-

克里斯。



Thanks for posting this (but, be prepared for others shouting "send it
to comp.programming").

<politely> As this stands, and without API facility to iterate across a
row, or down a column, what does your dynamically allocated associative
array provide that a hashtable wouldn''t provide?

--
Chris.


6月10日星期六2006 01:33:49 +0000,Richard Heathfield写道:
On Sat, 10 Jun 2006 01:33:49 +0000, Richard Heathfield wrote:
tabl = malloc(sizeof(tabl-> number_of_elements)+ sizeof(tabl-> column1)
+
sizeof(tabl-> column2));

tabl-> column1 = malloc(sizeof(tabl-> column1));
钟声!钟声!
tabl = malloc(sizeof(tabl->number_of_elements) + sizeof(tabl->column1)
+
sizeof(tabl->column2));

tabl->column1 = malloc(sizeof(tabl->column1));
The bells! The bells!




我其实不是那个C大师,但我希望这是正确的(虽然丑陋

看)。你能告诉我这个代码有什么危险吗?或许你想给我讲一些讲座吗?


2006年6月10日星期六01:42:42 +0000 ,克里斯麦克唐纳写道:感谢发布这个(但是,准备好让其他人大喊发送它
到comp.programming)


新用户网帖,对不起。

< politely>由于这一点,并且没有API工具来遍历
行或向​​下列,您的动态分配的关联
数组提供的哈希表无法提供什么?



I''m actually not that C guru, but I expect this to be correct (though ugly
looking). Could you tell me whats dangerous about this code or perhaps
point me to some lecture?

On Sat, 10 Jun 2006 01:42:42 +0000, Chris McDonald wrote: Thanks for posting this (but, be prepared for others shouting "send it
to comp.programming)
New to Usenet posting, sorry for that.
<politely> As this stands, and without API facility to iterate across a
row, or down a column, what does your dynamically allocated associative
array provide that a hashtable wouldn''t provide?




*经过一番调查*

几乎没有。

所以我想知道为什么人们一直在问这个问题,我什么也没想到比如

这个存在于C中而且从不需要它。


反正我的好习惯。

-

/〜\ ascii ribbon campaign for html email

\ /和专有附件!

X

/永远不会将.doc .xls或.ppt文件附加到电子邮件中!



*after some investigation*
Approximately nothing.
So I''m wondering why people keep asking about this, I assumed nothing like
this to exist in C and never needed it myself.

Good practice for me anyways.
--
/~\ the ascii ribbon campaign against html email
\ / and proprietary attachments!
X
/ \ never attach .doc .xls or .ppt files to emails!


这篇关于C中动态分配的关联数组的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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