C equivalent of C++ STL
Solution 1
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: http://developer.gnome.org/glib/stable/
GNU Gnulib: http://www.gnu.org/software/gnulib/
Solution 2
glib does include GHashTable
s 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.
Solution 3
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.
Related videos on Youtube
CodeKingPlusPlus
Updated on July 13, 2022Comments
-
CodeKingPlusPlus almost 2 years
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!
-
Drew Delano almost 12 yearsLots of similar questions: stackoverflow.com/search?q=[c]+data+structure+library
-
-
Daniel Kamil Kozar almost 12 yearsGlib is a great library for just that.
-
kesarling He-Him over 4 yearsThat's ignorance and prejudice speaking, not you. C being a turing complete language can always have anything and everything C++ provides, though maybe not how C++ provides. :)
-
Myst almost 4 yearsIt isn't hard to utilize macros in order to generate code and types (see, for example the facil.io C STL library). C is more versatile than most people give it credit (and it has better encapsulation, if you ask me).
-
paulsm4 almost 4 yearsIt's so refreshing to hear someone say this:
C is more versatile than most people give it credit (and it has better encapsulation, if you ask me).
I agree completely :)