https://github.com/swenson/vector.h
C header library for typed lists (using macros and "template" C).
https://github.com/swenson/vector.h
Last synced: 10 months ago
JSON representation
C header library for typed lists (using macros and "template" C).
- Host: GitHub
- URL: https://github.com/swenson/vector.h
- Owner: swenson
- License: mit
- Created: 2014-05-05T23:18:24.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-05-05T23:29:26.000Z (over 11 years ago)
- Last Synced: 2025-03-18T23:41:35.305Z (10 months ago)
- Language: C
- Size: 137 KB
- Stars: 47
- Watchers: 3
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-c - vector.h - Header library for typed lists. [MIT](https://spdx.org/licenses/MIT.html) (Data Structures)
- awesome-c-zh - vector.h - 类型化列表的头库。[](https://spdx.org/licenses/MIT.html) (数据结构)
- awesome-c - vector.h - Header library for typed lists. [MIT](https://spdx.org/licenses/MIT.html) (Data Structures)
README
# vector.h
C header library for typed lists (using macros and "template" C).
Essentially, this is a resizable array of elements of your choosing that
is automatically managed by a few simple functions.
Similar in spirit to [sort.h](https://github.com/swenson/sort).
## Instructions
Download `vector.h` and put it somewhere in your `-I` include path.
Define the `VECTOR_TYPE` macro with the type you want your vector to be of.
Like, `int`, `float`, etc.
Define the `VECTOR_NAME` to be the "name" of the vector type, in that,
all functions will look like `vec_VECTOR_NAME_somefun`. (See below.)
`#include "vector.h"`
For example, if your `VECTOR_NAME` is `int`, then you now have `vec_int_t`
as your type.
You can pass them around normally as value types, but you always need to
`init` them.
```c
vec_int_t myvec;
vec_int_init(myvec);
```
You also have access to all the functions you need, including:
* `init` (initialize an `vec_whatever_t`)
* `get`
* `pop` (remove and return the last element)
* `del` (delete an element anywhere in the vector;
WARNING: could rewrite the whole list.)
* `size`
* `append`
## Sample usage
```c
#include
#define VECTOR_TYPE int
#define VECTOR_NAME int
#include "vector.h"
int main(void) {
int i;
vec_int_t vec;
vec_int_init(vec);
vec_int_append(vec, 2);
vec_int_append(vec, 100);
vec_int_pop(vec);
vec_int_del(vec, 0);
for (i = 0; i < vec_int_size(vec); i++) {
printf("%d\n", vec_int_get(vec, i));
}
}
```