A simple growable vector for C.
This library provides a simple growable vector with many different manipulation functions.
#include "vector.h"
#include <stdio.h>
int main()
{
// create new vector which can auto grow as needed.
// Can also specify initial size and if allowed to resize using
// the following: vector_new_with_options(size, allow_resize)
struct Vector *vector = vector_new();
// populate vector using multiple available functions
vector_push(vector, "test push");
vector_insert(vector, 1, "test insert"); // shifts all items from index 1 forward
vector_prepend(vector, "test prepend");
char *previous_value = (char *)vector_set(vector, 1, "test set"); // replaces the item at index 1
printf("Replaced value at index 1, old value: %s\n", previous_value);
// can fetch any item
char *value = (char *)vector_get(vector, 1);
printf("Value at index 1: %s\n", value);
value = (char *)vector_pop(vector);
printf("Last Value: %s\n", value);
// or fetch all items
void **all_items = vector_to_array(vector);
printf("First item: %s\n", (char *)all_items[0]);
// can remove any item
value = (char *)vector_remove(vector, 0); // shifts all items after index backward
printf("Removed first item: %s\n", value);
// modify the vector size
size_t size = vector_size(vector);
size_t capacity = vector_capacity(vector);
printf("Current size: %zu capacity: %zu\n", size, capacity);
vector_shrink(vector);
size = vector_size(vector);
capacity = vector_capacity(vector);
printf("Current size: %zu capacity: %zu\n", size, capacity);
vector_shrink(vector);
size = vector_ensure_capacity(vector, 100);
capacity = vector_capacity(vector);
printf("Current size: %zu capacity: %zu\n", size, capacity);
// when we are done with the vector, we release it
vector_release(vector);
} /* main */
See Changelog
Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.