在C中创建关联数组的最佳方法是什么? [英] What's the best way to create an associative array in C?

查看:98
本文介绍了在C中创建关联数组的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我想创建一个查询表,我可以存储字符串键:


例如:


192.168.0.1 - >紫色

192.168.0.2 - >蓝色

192.168.0.3 - >红色


第一个字段是IP地址 - 但基本上是文本字符串。

颜色 - 例如紫色,蓝色或红色是与IP地址相关的值

(键的类型)。


Angus Comber
an***@NOSPAMiteloffice.com

Hello

I want to create a lookup table where I can store string keys:

For example:

192.168.0.1 -> Purple
192.168.0.2 -> Blue
192.168.0.3 -> Red

The first field are IP addresses - but basically a text string. The
colour - eg Purple, Blue or Red is the value associated with the IP address
(sort of the key).

Angus Comber
an***@NOSPAMiteloffice.com

推荐答案

Angus Comber写道:
Angus Comber wrote:
你好

我想创建一个查找表,我可以存储字符串键:

例如:

192.168.0.1 - >紫色
192.168.0.2 - >蓝色
192.168.0.3 - >红色

第一个字段是IP地址 - 但基本上是文本字符串。
颜色 - 例如紫色,蓝色或红色是与IP地址相关的值
(键的类型)。
Hello

I want to create a lookup table where I can store string keys:

For example:

192.168.0.1 -> Purple
192.168.0.2 -> Blue
192.168.0.3 -> Red

The first field are IP addresses - but basically a text string. The
colour - eg Purple, Blue or Red is the value associated with the IP address
(sort of the key).




C不提供开箱即用的关联数组支持。你需要使用

一个合适的算法从低级别的东西实现这种东西。


如果你不喜欢坚持自己滚动,看看libjudy

http://sourceforge.net/projects/judy/)


请注意,您调用与IP地址关联的值排序

关键词有点混乱;在普通客厅中,被用作

索引的东西(在您的情况下,IP地址)被称为密钥。


Best问候,


Sidney



C does not provide support for associative arrays out of the box. You
will have to implement this kind of thing from lower-level things using
an appropriate algorithm.

If you don''t insist on rolling-your-own, take a look at libjudy
(http://sourceforge.net/projects/judy/).

Note that your calling the value associated with the IP address "sort of
the key" is a bit confusing; in normal parlor, the thing being used as
index (in your case, the IP address) is referred to as the "key".

Best regards,

Sidney




2004年2月2日星期一,安格斯Comber写道:

On Mon, 2 Feb 2004, Angus Comber wrote:

[在C中创建关联数组的最佳方法是什么?]


在以后的帖子中,请不要开始编写消息,直到

将光标移动到消息正文中。您的

帖子的第一行似乎已经在主题行中结束了。 [我知道它已经打算留在邮件正文中了,因为它

包含与你的问题相关的重要信息。]

我想创建一个查找表,我可以在其中存储字符串键:
例如:

192.168.0.1 - >紫色
192.168.0.2 - >蓝色
192.168.0.3 - >红色

第一个字段是IP地址 - 但基本上是文本字符串。
颜色 - 例如紫色,蓝色或红色是与IP地址相关的值(键的类型)。

[What''s the best way to create an associative array in C?]
In future posts, please don''t start writing your message until
you move the cursor to the message body. The first line of your
post seems to have ended up in the subject line. [I know that it
must have been intended to go in the message body, because it
contains important information relevant to your question.]

I want to create a lookup table where I can store string keys:
For example:

192.168.0.1 -> Purple
192.168.0.2 -> Blue
192.168.0.3 -> Red

The first field are IP addresses - but basically a text string. The
colour - eg Purple, Blue or Red is the value associated with the IP
address (sort of the key).




全部取决于你想要对条目做什么。只有

提供这么多信息,你几乎肯定会被淹没

与smartass的答案一致


const char * arr [3] [2] =

{

{" 192.168.0.1"," Purple" },

{" 192.168.0.2"," Blue" },

{" 192.168.0.3"," Red" },

};


那么,你想要一个完整的关联数组实现,比如

可以用的东西实现Perl?试试哈希表。

您是否只想存储一些地址和值,例如20或

30?也许一个简单的阵列会更好。既然你知道

你所有的钥匙都是32位数字,也许你应该考虑一个带有四个等级的trie




enum颜色{红色,紫色,蓝色};


struct trienode

{

struct trienode * child [256];

enum颜色值;

};


有很多种方法可以在C中存储数据(和其他任何方式一样)

语言)。如果您对各种数据

结构的优点感兴趣,请在comp.programming中再次询问。但是如果你出来并且

告诉我们*你想做什么,以及*为什么*你想要这样做[

*为什么*非常重要] ,那么我肯定有人可以建议一个好的

方式在C中这样做。


-Arthur



It all depends on what you want to do with the entries. Only
providing this much information, you will almost certainly be deluged
with smartass answers along the lines of

const char *arr[3][2] =
{
{ "192.168.0.1", "Purple" },
{ "192.168.0.2", "Blue" },
{ "192.168.0.3", "Red" },
};

So, do you want a full associative-array implementation, like
something that could be used to implement Perl? Try a hash table.
Do you just want to store a few addresses and values, like 20 or
30? Maybe a simple array would be better. Since you know that
all your keys are 32-bit numbers, maybe you should consider a trie
with four levels:

enum Color { Red, Purple, Blue };

struct trienode
{
struct trienode *child[256];
enum Color value;
};

There are many, many ways to store data in C (as in any other
language). If you''re interested in the merits of various data
structures, ask again in comp.programming. But if you come out and
tell us *what* you want to do, and *why* you want to do it [the
*why* is very important], then I''m sure someone can suggest a good
way to do it in C.

-Arthur


On Mon,2004年2月2日23:07:15 -0000,Angus Comber

< an *** @ iteloffice.com.PLEASENOSPAM>在comp.lang.c中写道:
On Mon, 2 Feb 2004 23:07:15 -0000, "Angus Comber"
<an***@iteloffice.com.PLEASENOSPAM> wrote in comp.lang.c:
你好

我想创建一个查询表,我可以存储字符串键:
例如:

192.168.0.1 - >紫色
192.168.0.2 - >蓝色
192.168.0.3 - >红色

第一个字段是IP地址 - 但基本上是文本字符串。
颜色 - 例如紫色,蓝色或红色是与IP地址相关的值
(键的类型)。

Angus Comber
*** *** @ NOSPAMiteloffice.com




关于IP地址的好处是它们是保证在每个符合标准的C实现中将
放入unsigned long。


这取决于使用,但是如果有很多这些可以节省

存储将IP地址转换为无符号长度。


如果在程序启动时可以获得所有可能的值,请定义

结构类型有两个成员,一个unsigned long(IP地址),

和另一个成员是你需要的任何东西,比如指向

字符串的指针。填充它们的数组,qsort()它,然后你

可以bsearch()当你需要查找值时,这是一种非常有效的搜索技术。


如果需要能够一直添加,删除和更改项目,

各种其他数据结构可能比简单的

数组。在comp.programming中询问有关选择最佳

结构的帮助。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



The nice thing about IP addresses is that they are guaranteed to fit
into an unsigned long on every conforming C implementation.

It depends on use, but if there are a lot of these you could save
storage by converting the IP addresses into unsigned longs.

If all possible values are available at program start-up, define a
structure type with two members, an unsigned long (the IP address),
and the other member being whatever you need, such as pointer to
character string. Populate an array of them, qsort() it, and then you
can bsearch() it when you need to look up values, an extremely
efficient search technique.

If need to be able to add, delete, and change items all the time,
various other data structures might be more appropriate than a simple
array. Ask in comp.programming for help in selecting the best
structure.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


这篇关于在C中创建关联数组的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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