用C词典/地图/键值对数据结构 [英] dictionary/map/key-value pairs data structure in C
问题描述
一个人怎么构造,用C访问一组键 - 值对的?要使用一个愚蠢的简单的例子,比方说我要创造出一个整数,它的平方根之间转换的表。
How does one construct and access a set of key-value pairs in C? To use a silly simple example, let's say I want to create a table which translates between an integer and its square root.
如果我是编写JavaScript,我可以做到这一点:
If I were writing javascript, I could just do this:
var squareRoots = {
4: 2,
9: 3,
16: 4,
25: 5
}
,然后访问它们喜欢的:
and then access them like:
var squareRootOf25 = squareRoots[5]
什么是prettiest方式在C这样做吗?如果我想使用一种类型的枚举作为重点,并为价值的另一个枚举类型?
What's the prettiest way to do this in C? What if I want to use one type of enum as the key and another type of enum as the value?
推荐答案
您可以考虑在C语言中的散列实现这个功能。
哈希的基础参阅维基百科。
请参阅本<一个href=\"http://stackoverflow.com/questions/305611/container-class-library-for-c/306461#306461\">question有关详细信息和链接。
You could consider hash implementation in C language to achieve this. For basics of hash refer to Wikipedia. Refer to this question for more details and links.
这链接提供良好的概述和实施细则。
This link gives good overview and implementation details.
这篇关于用C词典/地图/键值对数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!