Wiki

Clone wiki

kick / vector

The Skinny

A vector is a dynamic, sequence container. All memory management for vector is handled by an allocator. By default, the allocator keeps track of two internal sizes - allocated size, and utilized size.

Allocated size referes to the total number of items that have been allocated for the vector. Utilized size referes to the total number of items that have been added to the vector. Each time an item is added, the allocator will make sure that enough memory has already been allocated for the new item. If not, it will allocate four additional spaces, one for the new item, and three for future items.

Container Features

  • Sequential - Items are ordered in a linear sequence and accessed the same as regular arrays
  • Dynamic - Items can be added/removed to and from anywhere in the vector
  • Allocation - Any vector can use custom allocators based on performance needs

Template Parameters

  • T - The object type of individual items
  • A - The allocator type for managing memory

Basic Usage

#include <iostream>
#include <kick/vector.h>

int main( int argc, char* argv[] ){
    kick::vector<int> myVector; 
    
    myVector.push_back( 100 ); 
    myVector.push_back( 200 ); 
    myVector.push_back( 300 ); 
    
    // Access individual items, outputs 100
    std::cout << myVector[0] << std::endl; 

    // Iterate by incrementing an index
    for( int i = 0; i < myVector.size(); ++i )
        std::cout << myVector[i] << std::endl; 
    

    // Iterate with the vector iterator
    for( kick::vector<int>::iterator it = myVector.begin(); it != myVector.end(); ++it )
        std::cout << *it << std::endl; 

}

Updated