1. Vector #

Created Sunday 03 May 2020

Uses:

#include<iostream>
#include<vector>

int main()
{
vector<int> v(5);
}

Q) What is an iterator? A) It is the pointer to an element within the range of the data structures.

  1. We can go to the next one by doing +=1
  2. We can access the element in the data structure by dereferencing.
  3. vec.begin() returns the starting element. vec.end() is the pointer **after **the last element in the DS.
  4. To make an iterator using
vector<int> :: iterator it = vec.begin();
// we can also just use auto

Vector is just another name of our Dynamic Array. Exactly the same as our implementation.


header: Declaration:

vector <data_type> v; 	static
vector <data_type>* v = new vector<int>;	//note the parentheses
vector <data_type>* v = new vector<int>(23);	//intial size is 23, not capacity
// Our size is 23, not capacity. i.e push_back() increases size to 24.

// We can specify initial capacity by providing an integer paremeter to the constructor. e.g vector pq(23); Remember that this inserts a garbage of type int, i.e size()==capacity().


Methods:

Learnt on my own:


Note: Just don’t try to break the abstraction. Advantages:

  1. Very space efficient. Least wastage.
  2. Fastest access.
  3. Doubling decreases the sporadic insertion time. Can be compensated with reserve().