1 min
Vectors are sequence containers that represent an array which can change its size. Vectors use contiguous storage locations for their elements. Unlike arrays, they can change their size dynamically with their storage handled automatically by the container. Vectors do not reallocate the array each time an element is added to the container. Instead, they allocate extra storage to accommodate for any possible growth. Therefore they can have a capacity greater than the storage strictly needed to contain its elements. Compared to arrays, vectors consume more memory in exchange for the ability to automatically manage storage and grow dynamically in an efficient manner. Containers use different iterators for deciding how to navigate the array.
Because we want to be able to use memory dynamically but not have to worry about accidentally creating leaks in memory.
#inclue <vector>
int size = 10;
std::vector<int> vector_array(size); //make room for ten ints and initialise them
for (int i =0; i<size; i++)
{
array[i] = i;
}
When declaring a vector, you put the type inside <> and you can put a default size in the () following the name of the vector.
begin = Return iterator to beginning, end = Return iterator to end
size = Return size, max_size = Return maximum size, resize = Change size, capacity = Return size of allocated storage capacity, empty = Test whether vector is empty, reserve = Request a change in capacity
assign = Assign vector content, push_back = Add element at the end, pop_back = Delete last element, insert = Insert elements, erase = Erase elements, swap = Swap content, clear = Clear content
Subscribe to this blog via RSS.
Blog 4
Blog (4) Networking (1) Development (3) Code-snippets (1) Low-level programming (10) Nothing interesting (1) Snake game (5) Board games (3) Game adaptions (1) Play & games (2) Assignments (1) Level design (1) Endless runner (1) Birdman game (1) Group work (2) Networking game (1)