Skip to main content

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

T
typename
required
The element type to store in the vector
_Size
typename
default:"int"
The integer type used for size and capacity tracking

Type definitions

Size
_Size
Public typedef for the size type used by the vector

Constructors

Default constructor

vec()
Creates an empty vector with no allocated memory.

Size constructor

explicit vec(Size size)
size
Size
required
Initial size of the vector. Elements are default-constructed.

Size with padding constructor

vec(Size size, const T& pad)
size
Size
required
Initial size of the vector
pad
const T&
required
Value to copy into all elements

Size operations

size

Size size() const
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.
min_cap
Size
Minimum capacity to ensure is available

shrink

void shrink(Size nelems)
Removes nelems elements from the end of the vector and calls their destructors.
nelems
Size
required
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.
size
Size
required
Target size for the vector
pad
const T&
Value to copy into new elements (if provided)

clear

void clear(bool dealloc = false)
Removes all elements from the vector, calling their destructors.
dealloc
bool
default:"false"
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.
elem
const T&
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.
elem
const T&
required
Element to add to the vector
This method does not check capacity. Ensure size() < capacity() before calling.

pop

void 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.
index
Size
required
Zero-based index of the element to access

operator T*

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.
copy
vec<T>&
required
Destination vector (will be cleared first)

moveTo

void moveTo(vec<T>& dest)
Moves this vector’s contents to another vector, leaving this vector empty.
dest
vec<T>&
required
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

Performance characteristics

  • 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)

Build docs developers (and LLMs) love