可能的重复:
C 模拟到 STL
通常,在使用 C 进行编程时,我发现自己希望能够访问像 C++ 中的向量或列表类之类的东西,我最终实现了一些高级数据结构的精简版本,但这需要相当多的时间。我想知道是否有人知道 C 语言中有一个高质量的简单数据结构库?
Possible Duplicate:
C Analog To STL
Oftentimes, when programming in C, I find myself wishing that I had access to something like the vector or list classes from C++, and I end up implementing a sort of stripped-down version of some high-level data structure, but it takes a fair amount of time. I am wondering if anyone knows of a good library of high quality simple data structures in C?
发布评论
评论(3)
嗯,有 GLib 库,库,它用于实现 GTK+ 小部件集。 GLib 使用 GObject 作为在 C 中进行 OOP 的一种方式。但是 GObjectified C 因其非常强大而闻名。冗长。
Well, there's the GLib library, library, which is what is used to implement the GTK+ widget set. GLib uses GObject as a way to do OOP in C. But GObjectified C is famous for being very verbose.
除了其他人建议的 GLib 之外,您还可以创建自己的通用类型,而无需任何类型安全,例如使用
void *
作为指向下一个堆栈对象的通用指针的链接列表。有些人甚至使用宏在 ANSI C 中创建类似模板的行为,并取得了一定的成功。在 cplusplus.com 的论坛中搜索一两个示例(如果我没记错的话,没有更多的例子)。或者,您可以实现自己的 OOP 方案,如论文 使用 ANSI C 进行面向对象编程< /a>,尽管我不推荐这样做。您不妨花时间创建这个野兽(并且可能不了解它最终如何工作)并学习一些 GLib 来熟悉它。
我个人更喜欢第一种方法。您会失去类型安全性并获得大量强制转换,但它是实现速度最快的。该方法的真正问题是存储的数据。转换为 C 的
std::stack
只是一个具有 int 类型数据字段的结构体 stack。std::vector 怎么样? >
,整数堆栈的可寻址列表? C 标准不能保证从指针类型到整型的转换不会丢失一些信息。换句话说,虽然您必须使用指针,但不能使用普通整数来存储信息。也许一些展开的模板特化会起作用——一种用于 int,(一种用于 long long?),一种用于 double,一种用于指针类型。最后,最好的解决方案是使用像 GLib 这样的库,但如果您更喜欢自己开发,请在创建它时小心。您永远不知道什么时候需要在容器中组合不同类型的物品。
Other than GLib as others have suggested, you could create your own generic types without any type safety, such as a linked list that uses
void *
as the generic pointer to the next stack object. Some have even used macros to create template-like behaviour in ANSI C with some reasonable success. Search around cplusplus.com's forums for an example or two (there aren't many more than that if I recall correctly).Alternatively you could implement your own OOP scheme as described in the paper Object-Oriented Programming With ANSI C, though it's something I wouldn't recommend. You might as well take the time you'd spend creating that beast (and possibly not understanding how it works in the end) and learn some of GLib to get familiar with it.
Personally I prefer the first approach. You lose type safety and gain a bunch of casts, but it's the fastest to implement. The real problem with that method is the data that is stored.
std::stack<int>
converted to C is simply a struct stack that has a data field of type int. What aboutstd::vector<std::stack<int> >
, an addressable list of stacks of integers? The C standard cannot guarantee that casting from a pointer type to an integral type will work without some loss of information. In other words, while you can necessarily use pointers, you can't use plain integers to store information. Perhaps a few unrolled template specializations would work -- one for int, (one for long long?), one for double, and one for pointer types.In the end, the best solution is to use a library like GLib, but if you prefer to roll your own, take care when creating it. You never know when you'll need a combination of types of items in a container.
我喜欢 GLib 来做这种事情;您最终编写的代码是干净的,库易于使用、稳定、成熟和跨平台,并且文档非常出色。
您可能还想看看 Rusty Russell 的 CCAN 项目。我没有使用其中的任何代码,但下次当我有类似的需求时,我肯定会尝试一下:
http: //ccan.ozlabs.org/
I like GLib for this kind of thing; the code you end up writing is clean, the libraries are easy to use, stable, mature and cross-platform, and the documentation is excellent.
You may also want to take a look at Rusty Russell's CCAN project. I haven't used any code from it but I will definitely go dip my toe in the next time I have similar needs:
http://ccan.ozlabs.org/