将已知的键/值对存储在c中 [英] store known key/value pairs in c

查看:48
本文介绍了将已知的键/值对存储在c中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习c.我正在编写Web服务器作为练习.
现在我必须存储状态码和原因短语.

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.

存储这些键/值对的最佳方法是什么?

What is the best way to store those key/value pairs?

我的第一个赌注是一个哈希图.但是c中没有本机实现.所以我将不得不使用图书馆.

My first bet was a hashmap. But there is no native implementation in c. So i would have to use a library.

推荐答案

与其他答案一样,我也建议仅使用字符串数组作为查找表.如果假设所有状态码都是唯一的,那么对于较小的数据集,字符串数组是迄今为止最简单的实现.

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.

一旦开始存储大量数据,即哈希图开始变得有用.查找数组是此处的解决方案,但是正如您所说的,您在学习C时,实际上可以通过使用动态内存在本地C中实现哈希表(学习C的关键概念.)该网站介绍了如何在其中创建哈希表. C很好.

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.

http://www.sparknotes.com/cs/searching/hashtables/section3.rhtml

这篇关于将已知的键/值对存储在c中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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