A vector is a dynamic array data structure that provides flexible storage and efficient insertion and deletion of elements. It automatically manages memory allocation and deallocation, allowing the vector to grow and shrink as needed.
struct DSCVector;
Represents a vector. The internal implementation of the DSCVector
struct is hidden and defined in the source file.
DSCVector *dsc_vector_create();
Creates a new empty vector.
Returns: A pointer to the newly created vector, or NULL
if memory allocation fails.
Example:
DSCVector *vector = dsc_vector_create();
if (vector == NULL) {
// Handle memory allocation failure
}
void dsc_vector_free(DSCVector *vector);
Destroys the vector and frees its memory.
Parameters:
vector
: The vector to destroy.
Example:
DSCVector *vector = dsc_vector_create();
// Use the vector
dsc_vector_free(vector);
void dsc_vector_push_back(DSCVector *vector, int value);
Inserts an element at the end of the vector.
Parameters:
vector
: The vector to push the element into.value
: The value to push.
Example:
DSCVector *vector = dsc_vector_create();
dsc_vector_push_back(vector, 42);
dsc_vector_push_back(vector, 73);
void dsc_vector_pop_back(DSCVector *vector);
Removes the last element from the vector.
Parameters:
vector
: The vector to pop from.
Example:
DSCVector *vector = dsc_vector_create();
dsc_vector_push_back(vector, 42);
dsc_vector_push_back(vector, 73);
dsc_vector_pop_back(vector); // Removes 73 from the vector
void dsc_vector_insert(DSCVector *vector, int value);
Inserts an element at the beginning of the vector.
Parameters:
vector
: The vector to insert the element into.value
: The value to insert.
Example:
DSCVector *vector = dsc_vector_create();
dsc_vector_insert(vector, 42);
dsc_vector_insert(vector, 73);
void dsc_vector_erase(DSCVector *vector);
Removes the first element from the vector.
Parameters:
vector
: The vector to remove the first element from.
Example:
DSCVector *vector = dsc_vector_create();
dsc_vector_insert(vector, 42);
dsc_vector_insert(vector, 73);
dsc_vector_erase(vector); // Removes 42 from the vector
int dsc_vector_at(const DSCVector *vector, unsigned int index);
Retrieves the value of the element at the specified index.
Parameters:
vector
: The vector to get the element from.index
: The index of the element to retrieve.
Returns: The value of the element at the specified index, or 0 if the index is out of bounds.
Example:
DSCVector *vector = dsc_vector_create();
dsc_vector_push_back(vector, 42);
dsc_vector_push_back(vector, 73);
int value = dsc_vector_at(vector, 1); // value = 73
bool dsc_vector_empty(const DSCVector *vector);
Checks if the vector is empty.
Parameters:
vector
: The vector to check.
Returns: true
if the vector is empty, false
otherwise.
Example:
DSCVector *vector = dsc_vector_create();
if (dsc_vector_empty(vector)) {
printf("The vector is empty\n");
} else {
printf("The vector is not empty\n");
}
unsigned int dsc_vector_size(const DSCVector *vector);
Gets the number of elements in the vector.
Parameters:
vector
: The vector to get the size of.
Returns: The number of elements in the vector.
Example:
DSCVector *vector = dsc_vector_create();
dsc_vector_push_back(vector, 42);
dsc_vector_push_back(vector, 73);
unsigned int size = dsc_vector_size(vector); // size = 2
unsigned int dsc_vector_capacity(const DSCVector *vector);
Gets the maximum number of elements the vector can hold before needing to allocate more memory.
Parameters:
vector
: The vector to get the capacity of.
Returns: The maximum number of elements the vector can hold.
Example:
DSCVector *vector = dsc_vector_create();
dsc_vector_push_back(vector, 42);
dsc_vector_push_back(vector, 73);
unsigned int capacity = dsc_vector_capacity(vector); // capacity = 2 (or more, depending on the initial capacity)
void dsc_vector_reserve(DSCVector *vector, unsigned int new_capacity);
Reserves memory for the vector to hold at least the specified number of elements.
Parameters:
vector
: The vector to reserve memory for.new_capacity
: The minimum capacity to reserve.
Example:
DSCVector *vector = dsc_vector_create();
dsc_vector_reserve(vector, 10); // Reserve memory for at least 10 elements