C equivalent of C++ STL [duplicate]

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!

2

4 Answers

C can't have an "exact equivalent" of STL because C doesn't have templates or classes.

You might be interested in the "Glib collections" library:

  • Gnome Glib:

  • GNU Gnulib:

3

glib does include GHashTables which are basically associations between keys and values - what HashMap is in C++.

The important difference is that you have to use void* to store arbitrary data since C doesn't support templates or generics. The downside is that the compiler can't check the validity of your code and you have to ensure correctness on your own.

You can implement your own in C actually. Make a struct, give it a pointer to its parent and implement a function that returns a pointer to an instance of your struct and you have your classes in C. You can go as far as you want actually if you have the time and you know how to do it.

C could never have anything like that, because it doesn't have any of the required features- especially templates.

1

You Might Also Like