Overview
The vec<T> template class provides an automatically resizable array implementation optimized for performance. It uses realloc for memory management and grows by approximately 3/2 when capacity is exceeded.
Do not use this vector on datatypes that cannot be relocated in memory with realloc.
Template parameters
The element type to store in the vector
The integer type used for size and capacity tracking
Type definitions
Public typedef for the size type used by the vector
Constructors
Default constructor
Creates an empty vector with no allocated memory.
Size constructor
Initial size of the vector. Elements are default-constructed.
Size with padding constructor
vec(Size size, const T& pad)
Initial size of the vector
Value to copy into all elements
Size operations
size
Returns the number of elements in the vector.
capacity
int capacity() const
void capacity(Size min_cap)
Gets or sets the minimum capacity. When setting capacity, the vector grows by approximately 3/2 if needed.
Minimum capacity to ensure is available
shrink
Removes nelems elements from the end of the vector and calls their destructors.
Number of elements to remove (must be ≤ current size)
shrink_
void shrink_(Size nelems)
Fast version of shrink that does not call destructors. Use only when element destructors are trivial.
This method does not call destructors. Only use for types with trivial destructors.
growTo
void growTo(Size size)
void growTo(Size size, const T& pad)
Grows the vector to the specified size. New elements are either default-constructed or initialized with pad.
Target size for the vector
Value to copy into new elements (if provided)
clear
void clear(bool dealloc = false)
Removes all elements from the vector, calling their destructors.
If true, also frees the allocated memory
Stack interface
push
void push()
void push(const T& elem)
Adds an element to the end of the vector. The no-argument version default-constructs the element.
Element to add to the vector
Overflow cannot occur in the sz+1 expression because capacity is always even but INT_MAX is odd.
push_
void push_(const T& elem)
Fast push that assumes sufficient capacity is available.
Element to add to the vector
This method does not check capacity. Ensure size() < capacity() before calling.
pop
Removes and destructs the last element. Vector must not be empty.
last
T& last()
const T& last() const
Returns a reference to the last element. Vector must not be empty.
Vector interface
operator[]
T& operator[](Size index)
const T& operator[](Size index) const
Accesses the element at the given index. No bounds checking is performed.
Zero-based index of the element to access
operator T*
Provides implicit conversion to pointer to first element.
Duplication operations
copyTo
void copyTo(vec<T>& copy) const
Creates a deep copy of this vector.
Destination vector (will be cleared first)
moveTo
void moveTo(vec<T>& dest)
Moves this vector’s contents to another vector, leaving this vector empty.
Destination vector (will be cleared first)
Usage example
#include "minisat/mtl/Vec.h"
using namespace Minisat;
vec<int> numbers;
numbers.push(10);
numbers.push(20);
numbers.push(30);
for (int i = 0; i < numbers.size(); i++) {
printf("%d\n", numbers[i]);
}
numbers.pop();
printf("Last: %d\n", numbers.last()); // Prints: Last: 20
- Random access: O(1)
- Push operation: Amortized O(1)
- Pop operation: O(1)
- Growth factor: Approximately 3/2
- Memory overhead: Minimal (three words: data pointer, size, capacity)