This library provides a dynamic array (vector) implementation in C, allowing you to create and manipulate vectors of various types with dynamic resizing capabilities.
The vector.h
header file uses a macro to define a vector type for a given data type. It provides functions to initialize a vector, add elements, access elements, and free the vector's resources.
This macro defines a vector type for the specified data type type
and name name
. It generates the following functions:
name *name##Init(size_t initial_capacity)
: Initializes a vector with an initial capacity.void name##Free(name *vector)
: Frees the memory allocated for the vector.void name##Add(name *vector, type value)
: Adds an element to the vector, resizing it if necessary.type name##Get(name *vector, size_t index)
: Retrieves an element at the specified index.
Include vector.h
in your source file:
#include "vector.h"
Use the DEFINE_VECTOR_TYPE
macro to create a vector type for your desired data type. For example, to create a vector for int
:
DEFINE_VECTOR_TYPE(int, IntVector);
Create an instance of the vector using the Init
function:
IntVector *vector = IntVectorInit(4); // Initial capacity of 4
Add elements to the vector using the Add
function:
IntVectorAdd(vector, 10);
IntVectorAdd(vector, 20);
IntVectorAdd(vector, 30);
Retrieve elements from the vector using the Get
function:
int value = IntVectorGet(vector, 0);
printf("Element at index 0: %d\n", value);
Free the allocated memory when done:
IntVectorFree(vector);
Here's a complete example demonstrating the use of the vector library:
#include "vector.h"
#include <stdio.h>
int main() {
// Define a vector of integers
DEFINE_VECTOR_TYPE(int, IntVector);
// Initialize the vector
IntVector *vector = IntVectorInit(4);
// Add elements to the vector
IntVectorAdd(vector, 10);
IntVectorAdd(vector, 20);
IntVectorAdd(vector, 30);
// Access and print elements
printf("Element at index 0: %d\n", IntVectorGet(vector, 0));
printf("Element at index 1: %d\n", IntVectorGet(vector, 1));
printf("Element at index 2: %d\n", IntVectorGet(vector, 2));
// Free the vector
IntVectorFree(vector);
return 0;
}
This library is provided under the MIT License. See the LICENSE file for more details.