只是因为我很好奇——C++ 中是否有与 STL 功能类似的 C 语言? 我见过提到一个名为 glib 的 GTK+ 库,一些人认为它满足了要求,但是还有其他库吗?会在 C 中提供 STL 功能吗?
Just because I'm curious--is there any C analog to the functionality of the STL in C++? I've seen mention of a GTK+ library called glib that a few people consider fills the bill but are there other libraries that would provide STL functionality in C?
发布评论
评论(4)
是的,glib 是一个非常好的选择:它包含许多用于操作容器的实用程序,如链表、数组、哈希表等。还有一个名为 GObject 的面向对象框架,您可以使用 C 语言创建带有信号和槽的对象(尽管使用相当冗长的函数调用名称,例如
gobject_set_property
,因为 C 没有任何对象语法)。 还有主循环的代码,因此您可以编写事件驱动的程序。有关详细信息,请参阅维基百科:https://en.wikipedia.org/wiki/GLib
Glib最初是 GTK 的一部分,但非 GUI 代码已被完全分解,以便您可以在命令行程序中使用它: http://library.gnome.org/devel/glib/stable/
Yes,
glib
is a pretty good choice: it includes a lot of utilities for manipulating containers like linked lists, arrays, hash tables, etc. And there is also an object-oriented framework calledGObject
that you can use to make objects with signals and slots in C (albeit with rather verbose function call names likegobject_set_property
, since C doesn't have any syntax for objects). And there is also code for main loops so you can write event-driven programs.For more info see wikipedia: https://en.wikipedia.org/wiki/GLib
Glib was originally part of GTK, but the non-GUI code has been completely factored out so that you can use it in command-line programs: http://library.gnome.org/devel/glib/stable/
CLIB
CLIB
添加另一个选项(完全公开,我是作者); 如果您可以编译和链接 C++,您可以查看
libcdada
,它具有纯 C API,但使用libstdc++
作为大多数容器的后端:< a href="https://github.com/msune/libcdada" rel="nofollow noreferrer">https://github.com/msune/libcdada
Adding another option (full disclosure, I am the author); if you can compile and link C++, you can have a look into
libcdada
, which has a pure C API, but useslibstdc++
as a backend for most of the containers:https://github.com/msune/libcdada
好吧,由于 STL 的本质是基于 C 所没有的模板,因此很难接近 C 中的 STL。您最好希望的是一些集合类,它们操纵指向未知对象的 void* 指针。
Well since STL's very nature is based on templates which C doesn't have, it would be difficult to even come close to the STL in C. The best you could hope for is some collection classes which manipulate void* pointers to unknown object.