C语言 为什么 C 标准库中没有哈希表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6118539/
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
Why are there no hashtables in the C standard library?
提问by Shankar Raju
Why is that there is no Hashtable support as part of Standard C Library? Is there any specific reason for this?
为什么标准 C 库中没有 Hashtable 支持?这有什么具体原因吗?
采纳答案by paxdiablo
There is no hashtable in the standard C library because either:
标准 C 库中没有哈希表,因为:
- no-one has submitted a proposal to the working group; or
- the working group has deemed it unnecessary.
- 没有人向工作组提交提案;或者
- 工作组认为没有必要。
That's the way ISO works. Proposals are put forward and accepted or rejected.
这就是 ISO 的工作方式。提议被提出并被接受或拒绝。
You have to be careful what you add to the standard library since you have two conflicting groups. As a user,you might want every data structure under the sun to be added to the standard to make the language more useful.
您必须小心添加到标准库中的内容,因为您有两个相互冲突的组。作为用户,您可能希望将阳光下的每个数据结构都添加到标准中,以使该语言更有用。
But, as a language implementor(as an aside, these are probably the people that tend to make up most of the various working groups so their view is likely to have more impact), you don't really want the hassle of having to implement stuff that may not be used by everyone. All the stuff that was there when C89 appeared was to do with the fact that the primary purpose was to codify existing practice rather than introduce new practices. All iterations of the standards since then have been a little freer in what they can do but backwards compatibility is still an important issue.
但是,作为语言实现者(顺便说一句,这些人可能往往构成了各种工作组的大部分,因此他们的观点可能会产生更大的影响),您真的不想要必须实现的麻烦可能不是每个人都使用的东西。当 C89 出现时,所有的东西都与这样一个事实有关,即主要目的是编纂现有的实践而不是引入新的实践。从那时起,标准的所有迭代都在他们可以做的事情上更加自由,但向后兼容性仍然是一个重要问题。
Myself, I also have conflicts. I'd love to have all the features of the Java, C++ or Python libraries at my disposal in C. Of course, that would make it so much harder to learn everything for newcomers and, as one commenter stated, probably make it so any code monkey can pump out useful code, reducing my value in the process :-)
我自己,也有矛盾。我很想在 C 中使用 Java、C++ 或 Python 库的所有功能。当然,这会让新手学习所有东西变得更加困难,正如一位评论者所说,可能会让它变得如此代码猴子可以抽出有用的代码,减少我在这个过程中的价值:-)
And I pretty much haveall the data structures I'll ever need, from my long and (mostly) illustrious career. You're not limited to the standard library for this sort of stuff. There are plenty of third-party tools you can get to do the job and (like me) you can also roll your own.
而且我几乎拥有我需要的所有数据结构,来自我漫长而(主要是)杰出的职业生涯。您不仅限于此类内容的标准库。您可以使用许多第三方工具来完成这项工作,并且(像我一样)您也可以使用自己的工具。
If you want to know why certain decisions were made in each iteration, ISO (and ANSI originally, before ISO took over) usually publish rationale documents. The C89 one from ANSI can be found here. It contains this little beauty in the scope:
如果你想知道为什么在每次迭代中做出某些决定,ISO(和最初的 ANSI,在 ISO 接管之前)通常会发布基本原理文档。ANSI 的 C89 可以在这里找到。它包含了这个范围内的小美:
This Rationale focuses primarily on additions, clarifications, and changes made to the language as described in the Base Documents. It is not a rationale for the C language as a whole: the Committee was charged with codifying an existing language, not designing a new one. No attempt is made in this Rationale to defend the pre-existing syntax of the language, such as the syntax of declarations or the binding of operators.
本基本原理主要侧重于对基础文档中描述的语言进行的添加、说明和更改。这不是整个 C 语言的基本原理:委员会负责编纂现有语言,而不是设计一种新语言。本基本原理中没有试图捍卫语言的预先存在的语法,例如声明的语法或运算符的绑定。
I especially enjoy the admission that they're not responsible for any unholy mess that may have predated their attempts to standardise.
我特别喜欢承认他们不对可能在他们尝试标准化之前出现的任何不洁的混乱负责。
But, perhaps the real answer to your question lies in this bit, one of the guiding principles:
但是,也许您的问题的真正答案在于这一点,指导原则之一:
Keep the spirit of C.The Committee kept as a major goal to preserve the traditional spirit of C. There are many facets of the spirit of C, but the essence is a community sentiment of the underlying principles upon which the C language is based. Some of the facets of the spirit of C can be summarized in phrases like:
保持 C 的精神。委员会将保持C的传统精神作为主要目标。C 精神有很多方面,但本质是社区对 C 语言所基于的基本原则的看法。C 精神的某些方面可以用以下短语来概括:
- Trust the programmer.
- Don't prevent the programmer from doing what needs to be done.
- Keep the language small and simple.
- Provide only one way to do an operation.
- Make it fast, even if it is not guaranteed to be portable.
- 相信程序员。
- 不要阻止程序员做需要做的事情。
- 保持语言小而简单。
- 仅提供一种操作方式。
- 让它快速,即使不能保证它是便携的。
That third one is probably the main reason why the library wasn't massively expanded with the initial standardisation effort - that, and the fact that such an expansion from a committee would probably have resulted in ANSI C being labeled C2038 rather than C89.
第三个可能是图书馆没有在最初的标准化工作中大规模扩展的主要原因——而且事实上,委员会的这种扩展可能会导致 ANSI C 被标记为 C2038 而不是 C89。
回答by Dietrich Epp
C seems unusual by today's standards because there are no useful data structures defined. None. Not even strings — and if you think a C string is a data structure, well, we'll have to disagree on what a "data structure" is.
按照今天的标准,C 似乎不寻常,因为没有定义有用的数据结构。没有任何。甚至不是字符串——如果你认为 C 字符串是一种数据结构,那么,我们将不得不在“数据结构”是什么方面存在分歧。
If you like C, then think of it as a "blank slate"... your entire application is made of code written by you and libraries you choose to pull in, plus a few fairly primitive standard library functions, with maybe one or two exceptions like qsort. People use C these days to implement things like Python, Ruby, Apache, or the Linux kernel. These are projects that use all of their own data structures anyway, and they wouldn't be likely to use something like the STL.
如果你喜欢 C,那么把它想象成一个“白板”……你的整个应用程序由你编写的代码和你选择引入的库组成,加上一些相当原始的标准库函数,可能只有一两个像qsort. 现在人们使用 C 来实现诸如 Python、Ruby、Apache 或 Linux 内核之类的东西。这些项目无论如何都使用他们自己的所有数据结构,而且他们不太可能使用像 STL 这样的东西。
Many C libraries implement generic hash tables. There are tradeoffs, and you can pick your favorite. Some of them are configurable using callbacks.
许多 C 库实现了通用哈希表。有一些权衡,你可以选择你最喜欢的。其中一些可以使用回调进行配置。
- Glib has a hash table object (documentation)
- Apache Portable Runtime has a hash table (documentation)
- Apple's Core Foundation library has a hash table (documentation) Note: Yes you can insert any object as key or value.
- UTHash is a hash table library (documentation)
- Another hash table library (link)
- Glib 有一个哈希表对象(文档)
- Apache Portable Runtime 有一个哈希表(文档)
- Apple 的 Core Foundation 库有一个哈希表(文档) 注意:是的,您可以插入任何对象作为键或值。
- UTHash 是一个哈希表库(文档)
- 另一个哈希表库(链接)
With all of these libraries that do what you want, what's the point of adding a hash table to the C standard?
所有这些库都可以满足您的需求,那么向 C 标准添加哈希表有什么意义呢?
回答by caf
The standard C library doesn't include anylarge, persistent data structures - neither lists, nor trees, nor stacks, nor hashtables.
标准 C 库不包含任何大型的、持久的数据结构——既不是列表,也不是树,也不是堆栈,也不是哈希表。
It's not really possible to give a definitive answer without asking the authors of the original C library. However, a plausible explanation is that the implementation of such data structures involves various tradeoffs, and only the author of the application is in the correct position to make those tradeoffs.
如果不询问原始 C 库的作者,就不可能给出明确的答案。然而,一个合理的解释是,这种数据结构的实现涉及各种权衡,只有应用程序的作者才能正确地进行这些权衡。
Note that the POSIX standard C library does specify generic hashtable functions: hcreate(), hsearch()and hdestroy(); and note also that their "one size fits all"nature tends to render them inadequate for most real-world use cases, supporting the argument above.
请注意,POSIX 标准 C 库确实指定了通用哈希表函数:hcreate(),hsearch()和hdestroy(); 还要注意,它们“一刀切”的性质往往使它们不适用于大多数现实世界的用例,支持上述论点。

