开发者

C Language Standard Collections Where Are They?

开发者 https://www.devze.com 2022-12-24 15:08 出处:网络
I have committed to learning C now, I\'m good with Python/PHP/Bash but I\'ve decided I\'m limited by not being fluent in C.However I cannot imagine working in a language without lists and hashes开发者

I have committed to learning C now, I'm good with Python/PHP/Bash but I've decided I'm limited by not being fluent in C. However I cannot imagine working in a language without lists and hashes开发者_如何学Python, maybe I'm just jumping a gun, but surely there are 'standard' collection libraries. I do not see any in the GNU standard lib though, any suggestions?


There is no "standard" set of collection classes for C. Many people simply roll their own as needed.

But of course, there are some libraries filling this gap. For example, glib offers linked lists, hashtables and various kinds of trees.


C is lower level than you're used to. There are no standard collections in C outside of the array.


Maybe you should try looking into glib. While it's not a standard in the same sense as STL for C++ it's a proven library and is used in a lot of applications.

http://library.gnome.org/devel/glib/2.22/


There is no standard, but there is a superb alternative that is far simpler than glib: Dave Hanson's C Interfaces and Implementations. It includes several efficient collection abstractions and a number of other useful modules. The software is free, and the book is worth buying.


Depending on your system, you may find what you're looking for in sys/queue.h, which includes "implementations of singly-linked lists, doubly-linked lists, simple queues, and tail queues".


The cdcontainers interface is similar to C ++ STL https://github.com/maksimandrianov/cdcontainers

#define CDC_USE_SHORT_NAMES  // for short names (functions and structs without prefix cdc_*)
#include <cdcontainers/vector.h>
#include <cdcontainers/casts.h>
#include <stdio.h>

int main(int argc, char** argv)
{
    vector_t *v;
    size_t i;

    if (vector_ctor(&v, NULL) != CDC_STATUS_OK)
        /* error handling */;

    if (vector_push_back(v, CDC_INT_TO_PTR(7)) != CDC_STATUS_OK)
        /* error handling */;

    if (vector_push_back(v, CDC_INT_TO_PTR(8)) != CDC_STATUS_OK)
        /* error handling */;

    for (i = 0; i < vector_size(v); ++i)
        printf("%i ", CDC_PTR_TO_INT(vector_get(v, i)));

    printf("\n");

    vector_dtor(v);

    return 0;
}


There aren't really standard collections in C. Being a very low-level language (compared to C++ and more "modern" languages)

C++ adds these via the Standard Template Library. Most of the "collections" such as hashing and lists are based on object oriented or generic programming techniques unavailable (other than via convention) in C.


There are no "standard" (as in part of the ISO C standard) container libraries, at least not as of C99. I've seen several third-party attempts; all had some degree of lossage or another.

C has a very primitive and meager toolkit; I've compared programming in C to building a house with nothing but a handsaw and a claw hammer.

0

精彩评论

暂无评论...
验证码 换一张
取 消