C语言 在 c 中存储已知的键/值对
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14731939/
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
store known key/value pairs in c
提问by Shylux
I'm currently learning c. I'm writing a web server as an exercise.
Now i have to store the status codes and reason phrases.
我目前正在学习 C. 我正在编写一个 Web 服务器作为练习。
现在我必须存储状态代码和原因短语。
What is the best way to store those key/value pairs?
存储这些键/值对的最佳方法是什么?
My first bet was a hashmap. But there is no native implementation in c. So i would have to use a library.
我的第一个赌注是哈希图。但是在 c 中没有本机实现。所以我将不得不使用图书馆。
采纳答案by Urchin
Like other answers, I would also recommend just using an array of strings as a lookup table. If you assume all the status codes are unique, an array of strings is by far the easiest implementation for a smaller set of data.
像其他答案一样,我也建议只使用字符串数组作为查找表。如果您假设所有状态代码都是唯一的,那么对于较小的数据集,字符串数组是迄今为止最简单的实现。
Once you start storing larger amounts of data, that is when hashmaps start becoming useful. A lookup array is the solution here, but as you said you're learning C, you can actually implement a hashtable in native C by using dynamic memory (a critical concept to learn for C.) This website explains how to create a hashtable in C very well.
一旦开始存储大量数据,哈希映射就开始变得有用了。查找数组是这里的解决方案,但正如您所说,您正在学习 C,实际上您可以通过使用动态内存(学习 C 的一个关键概念)在本机 C 中实现哈希表。此网站解释了如何在本机中创建哈希表C 很好。
http://www.sparknotes.com/cs/searching/hashtables/section3.rhtml
http://www.sparknotes.com/cs/searching/hashtables/section3.rhtml
回答by SirDarius
Here is an alternative idea, which has the benefit of speed, while having some memory overhead.
这是另一种想法,它具有速度优势,同时具有一些内存开销。
Basically, the simplest form of hash table, where the hash function is the identity (code -> code), also known as lookup table.
基本上是哈希表的最简单形式,其中哈希函数是身份(代码->代码),也称为查找表。
To do so, knowing that HTTP status codes are limited to 5xx, you can assume 599 will be the highest you need, therefore you will create a table with 600 elements.
为此,知道 HTTP 状态代码限制为 5xx,您可以假设 599 将是您需要的最高值,因此您将创建一个包含 600 个元素的表。
This table can be done like this:
这张表可以这样完成:
const char * status_messages[600];
Initialisation is pretty simple:
初始化非常简单:
/* initialize all with NULL's so invalid codes correspond to NULL pointers */
memset(status_messages, (int)NULL, 600 * sizeof(const char *));
/* ... */
status_messages[403] = "Forbidden";
status_messages[404] = "Not found";
/* ... */
Looking up a message is also dead-simple:
查找消息也非常简单:
int code = 403;
const char * message = status_messages[code];
This array will be 2400 bytes large (4800 on 64-bit platforms), but the access time is guaranteed to be O(1).
该数组将有 2400 字节大(64 位平台上为 4800),但访问时间保证为 O(1)。
回答by unwind
I would use a sorted array.
我会使用排序数组。
You can define the array in any order, and sort it at run-time (once) with the qsort()function. Then you can do binary searches using bsearch(). The total number of response codes is small, a binary search will be very fast.
您可以按任何顺序定义数组,并在运行时(一次)使用该qsort()函数对其进行排序。然后你可以使用bsearch(). 响应码的总数很少,二分查找会很快。
This has the advantage of not needing any external code, for something simple like this.
这具有不需要任何外部代码的优点,对于像这样的简单事情。
回答by Tony The Lion
Maybe you can create a struct with the K\V in it.
也许您可以创建一个包含 K\V 的结构。
Like so:
像这样:
struct key_value
{
int key;
char* value;
};
struct key_value kv;
kv.key = 1;
kv.value = "foo";

