A simple dynamic integer vector class implementation in C++.
IntVector is a dynamic integer array implementation that automatically grows its capacity when needed.
Creates an empty vector that does not allocate memory and has a capacity of 0.
Creates a vector with the specified capacity, allocating space for the given number of elements.
Frees the dynamically allocated memory and resets the vector's attributes to zero.
Fills the entire vector with zeros and sets the size equal to the capacity.
Fills the entire vector with ones and sets the size equal to the capacity.
Fills the vector with consecutive numbers starting from the specified value.
Adds an element to the end of the vector and automatically increases capacity if needed.
Inserts an element at the specified index, overwriting the previous value and filling intermediate gaps with zeros if necessary.
Returns the number of occurrences of the specified element in the vector.
Returns the index of the first occurrence of the specified element, or -1 if not found.
Prints to the console the vector's capacity, size, and all stored elements.
Returns the current number of elements stored in the vector.
Returns the vector's current capacity, meaning how many elements have allocated memory.
Changes the vector's capacity to the new value, preserving existing elements or truncating them if the new capacity is smaller.
Calculates and returns the sum of all elements stored in the vector.
Removes and returns the last element of the vector, decreasing the size by one.
iv::IntVector* v1 = new iv::IntVector(10);
v1->zeros();
v1->push_back(42);
v1->print();
delete v1;
Requires CMake ≥ 3.18 and a C++20 compiler. The build produces the Catch2 test executable.
mkdir build && cd build
cmake ..
make
./intvectorTests live in intvector_test.cpp and use Catch2 (amalgamated files included).
classDiagram
namespace iv {
class IntVector {
- int* array
- size_t size
- size_t capacity
+ IntVector()
+ IntVector(size_t capacity)
+ ~IntVector()
+ void zeros()
+ void ones()
+ void range()
+ void push_back()
+ void insert_override()
+ std::size_t count()
+ int index()
+ void print()
+ std::size_t get_size()
+ std::size_t get_capacity()
+ void resize(std::size_t new_capacity)
+ long int sum()
+ int pop_back()
}
}