C equivalent of C++ STL

20,073

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:

Solution 2

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.

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.

Share:
20,073

Related videos on Youtube

CodeKingPlusPlus
Author by

CodeKingPlusPlus

Updated on July 13, 2022

Comments

  • CodeKingPlusPlus
    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!

  • Daniel Kamil Kozar
    Daniel Kamil Kozar almost 12 years
    Glib is a great library for just that.
  • kesarling He-Him
    kesarling He-Him over 4 years
    That'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
    Myst almost 4 years
    It 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
    paulsm4 almost 4 years
    It'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 :)

Related