5

Possible Duplicate:
Container Class / Library for C

I expect I'd be forced to use vectors, lists and sets for my C program. Should I invent those entities from scratch, or there is some kind of standard library for C as STL is for C++ ?

Community
  • 1
  • 1
mbaitoff
  • 8,831
  • 4
  • 24
  • 32
  • 2
    duplicated question see for example: http://stackoverflow.com/questions/305611/container-class-library-for-c – quinmars Jan 06 '11 at 18:02
  • 1
    take a look at GLib: http://library.gnome.org/devel/glib/stable/glib-data-types.html – Christoph Jan 06 '11 at 18:27
  • If you want to use abstract vectors, lists, and sets then you should probably be writing C++, not C. Copying the idioms of a higher-level language onto C will defeat any advantage C has and will result in much uglier code than if you just wrote it in the higher-level language to begin with. See any `glib`/`gtk` code for a great example of this. – R.. GitHub STOP HELPING ICE Jan 06 '11 at 18:32
  • @Christoph: Haha your comment showed up just as soon as I added mine citing glib as why this is a bad idea. :-) – R.. GitHub STOP HELPING ICE Jan 06 '11 at 18:32
  • @R..: I've never used it extensively, but as far as I can tell, GLib is decent enough - it's GObject which tries to push square blocks through round holes; there's a reason why the GNOME guys created Vala as a frontend – Christoph Jan 06 '11 at 18:56

3 Answers3

2

C doesn't have templates, so it might be difficult to implement those C++ collections in a generic way. I'm not aware of any libraries that implement those features in C.

If I were faced with such a situation, my first thought would be to isolate the parts of my program that would benefit from C++ features, write them in C++, then provide an extern "C" interface to those modules so they could be called from the pure C parts of the program. Is that an option for you?

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
1

There is a standard c library, but it does not have any support for built-in container types such as the ones you list.

zdan
  • 28,667
  • 7
  • 60
  • 71
1

You can look at APR, or GLib. Those are widely used portable C libraries with everything you need not to reinvent the wheel each time.

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197