Home

Awesome

I've seen some people saying C++ is better than C because you don't have to handle memory allocation, things like std::vector does it for you. But the difference between C and C++ isn't in something like a std::vector or a std::list. They aren't part of the language syntax, they belongs to a mere library (yeah, that's the standard library, but that's still a library). From my point of view, C VS C++ is only about coding style: imperative or object programming? (plus a few fancy things like operator overloading or templates) And that's how I came to this project. C++ is nice because you have the STL containers? Here is the C STL, a C implementation of the STL.

This project is not a full implementation of the STL, it's rather a proof of concept: my goal isn't to provide a STL port in C, it's to prove that the STL containers aren't what makes C++ great, that they are totally doable in C. In that goal, the syntax have to be light, even if we need an heavy implementation behind to support this syntax.

Build

I may include some build scripts someday, but for now, gcc your_source_files.c cstl/*.c is the way to go.

Usage

Lots of use cases are shown in example.c. 2 containers are implemeted: vector and list. C doesn't have templates, so I dealt with macros instead. To use a container for a given type, define CSTL_NAME as the suffix of the container's name and CSTL_TYPE as the type inside the container, then include the corresponding header file. For example, if you want a vector of ints called vectornumber, define CSTL_TYPE to int and CSTL_NAME to number, then include vector.h. CSTL_TYPE and CSTL_NAME are automatically undefined, you can give them another value then re-include vector.h to define another vector.

To create a new container, declare a pointer to the container and call container_name_new(). For example:

vectornumber* v = vectornumber_new();

All containers have a delete member function:

v->delete();

Vector have a push_back member function:

v->push_back(3);

List have 2 member functions. If l is a list of ints:

l->push_front(3);
l->push_back(3);

All containers support for_each:

for_each(int, n, v, printf("%d\n", n));

The 4th parameter of for_each can either be an instruction or a block:

for_each(int, it, v,
{
    int var;
    var = it + 1;
    printf("%d + 1 = %d\n", it, var);
});

Compatibility notes

I tested this project under the following environments:

TODO