A good C equivalent of STL vector?

后端 未结 7 1904
悲&欢浪女
悲&欢浪女 2021-02-02 12:54

I\'ve noticed that at several places in our code base we use dynamically expanding arrays, i.e. a base array coupled with an element counter and a \"max elements\" value.

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-02 13:17

    Personally, I prefer "Gena" library. It closely resembles stl::vector in pure C89.

    It is comfortable to use because you can:

    • Access vector elements just like plain C arrays: vec[k][j];
    • Have multi-dimentional arrays;
    • Copy vectors;
    • Instantiate necessary vector types once in a separate module, instead of doing this every time you needed a vector;
    • You can choose how to pass values into a vector and how to return them from it: by value or by pointer.

    You can check it out here:

    https://github.com/cher-nov/Gena

提交回复
热议问题