Skip to content
Alex Krieg edited this page Nov 4, 2018 · 7 revisions

Functions

  • Vector();
  • Vector(unsigned int size,TYPE value);
  • ~Vector();
  • void push_back(T element);
  • TYPE pop_back();
  • TYPE& operator[](unsigned int pos);
  • Vector &operator=(Vector &vec);
  • Vector &operator+=(Vector &vec);
  • unsigned int size();
  • void resize(unsigned int size);
  • unsigned int capacity();
  • void erase(unsigned int pos,unsigned int amount = 1);
  • void clear();
  • TYPE* begin();
  • TYPE* end();


Vector();

Vector(unsigned int size = 0);

Description

  • Constructor
  • size predefined a size of the Array
  • No parameter will initialize the arraysize = 0

Syntax

Vector<int> myVector;
Vector<String> myVector;

  • You can choose any type as a Vector

~Vector();

Description

  • Deletes all saved data in the array.

Syntax

delete &myVector;


void push_back(T element);

Description

  • Adds an element to the array.

Syntax

myVector.push_back(15);


T pop_back();

Description

  • Returns the last last element of the array.
  • Deletes the last element in the array.
  • Decreses the size of the array.

Syntax

int value = myVector.pop_back();


Vector& operator[unsigned int pos];

Description

  • Returns the element of the array at pos.
  • If the pos is grater than the size of the array, it returns the pos % size.
  • If the size is 5, and pos is 7 than it returns element 2

Syntax

int value = myVector[0] This gets the first element of the array
int value = myVector[3] This gets the fourth element of the array
int value = myVector[myVector.size()-1] This gets the last element of the array


Vector &operator=(Vector &vec);

Description

  • Makes the left vector equal the right vector.
  • Both vectors have to be the same type.

Syntax

Vector<int> newVector = myVector;


Vector &operator+=(Vector &vec);

Description

  • Copys the right vector to the end of the left vector.
  • Both vectors have to be the same type.

Syntax

newVector += myVector;


unsigned int size();

Description

  • Gets the size of the array.

Syntax

unsigned int size = myVector.size();


void resize(unsigned int size);

Description

  • Resizes the array.
  • Used to make the push_back() faster.
  • Reservs storage for size elements.

Syntax

myVector.resize(10);


unsigned int capacity();

Description

  • returns the actual size of the array which is reserved in storage.

example

When an element is pushed back the size won't increase until capacity <= size.

Vector<int> myVector;
myVector.resize(5);  
myVector.size() == 0
myVector.capacity() == 5

myVector.push_back(1);
myVector.size() == 1
myVector.capacity() == 5

void erase(unsigned int pos,unsigned int amount = 1);

Description

  • Erase a sequence of elements in the array.
  • Beginning from element pos.
  • From there on deletes amount elements.

Syntax

myVector.erase(2,3); Erases the elements: [-]: [x][x][-][-][-][x][x]
myVector.erase(2); Erases the element: [-]: [x][x][-][x][x][x][x]


void clear();

Description

  • Clears the whole array.
  • All elements get deleted.

Syntax

myVector.clear();


T* begin();

Description

  • Gets the pointer of the first element in the array.
  • Don't use it in myVector.erase(myVector.begin());

Syntax

int *p_int = myVector.begin();


T* end();

Description

  • Gets the pointer of the last element in the array.
  • Don't use it in myVector.erase(myVector.end());

Syntax

int *p_int = myVector.end();