C Language Standard Collections Where Are They?

狂风中的少年 提交于 2020-01-02 00:32:12

问题


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, 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?


回答1:


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.




回答2:


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




回答3:


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/




回答4:


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.




回答5:


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.




回答6:


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.




回答7:


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".




回答8:


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;
}


来源:https://stackoverflow.com/questions/2502680/c-language-standard-collections-where-are-they

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!