18

Possible Duplicate:
Standard data structure library in C?

Does C have any data structure implementations similar to the C++ STL? Specifically associative containers, hash maps or any other structure with approximately constant time retrieval?

Thanks!

4

4 回答 4

28

C不能有 STL 的“完全等价物”,因为 C 没有模板或类。

您可能对“Glib 集合”库感兴趣:

于 2012-06-12T20:23:56.377 回答
6

glib确实包含GHashTables,它们基本上是键和值之间的关联——C++ 中的 HashMap 是什么。

重要的区别是您必须使用void*来存储任意数据,因为 C 不支持模板或泛型。缺点是编译器无法检查代码的有效性,您必须自己确保正确性。

于 2012-06-12T20:25:35.173 回答
2

您实际上可以在 C 中实现自己的。创建一个结构,给它一个指向其父级的指针并实现一个函数,该函数返回一个指向您的结构实例的指针,并且您在 C 中拥有您的类。如果您有时间并且知道的话,您可以去尽可能多的地方怎么做。

于 2012-10-05T20:03:10.387 回答
-3

C 永远不可能有这样的东西,因为它没有任何必需的特性——尤其是模板。

于 2012-06-12T20:27:11.273 回答