What is a vector class?
Vector class is a 1D dynamic array that allows infinite entries.
class Vector{
//private by default
T* arr;
int size; //current number of elements
int cap; //capacity
Public:
//other functions here
}
The vector class has functions like Push and Pop to insert and remove elements, respectively.
void push_back(const T & obj)
{
if (size < cap)
arr[size++] = obj;
else
{
int ncap = cap * 2;
T* temp = new T[ncap];
for (int i = 0; i < cap; i++)
temp[i] = arr[i];
temp[cap] = obj;
delete [] arr;
arr = temp;
size++;
cap = ncap;
}
}
void pop_back()
{
size--;
if (size < (cap/2))
{
T* temp = new T[cap / 2];
for (int i = 0; i < size; i++)
temp[i] = arr[i];
delete[] arr;
arr = temp;
cap = cap / 2;
}
}
These two functions go into the public section of the vector class. There are some other functions as well that I have included in my vector class.
Vector() //default constructor
{
cap = 256; //using 256 capacity by default
arr = new T[cap];
size = 0;
}
Vector (int capacity) //parameterized constructor
{
arr = new T[capacity];
cap = capacity;
size = 0;
}
void reallocate(int capacity) //reallocation function
{
delete[] arr;
arr = nullptr;
arr = new T[capacity];
cap = capacity;
size = 0;
}
Vector(const Vector <T> & obj) //copy constructor
{
arr = new T[obj.cap];
cap = obj.cap;
for (int i = 0; i < obj.size; i++)
{
arr[i] = obj.arr[i];
}
size = obj.size;
}
const Vector<T> & operator = (const Vector <T> & obj) //assignment operator overload
{
if (arr != nullptr)
delete[] arr;
arr = new T[obj.cap];
cap = obj.cap;
for (int i = 0; i < obj.size; i++)
{
arr[i] = obj.arr[i];
}
size = obj.size;
return obj;
}
The vector class has functions like Push and Pop to insert and remove elements, respectively.
void push_back(const T & obj)
{
if (size < cap)
arr[size++] = obj;
else
{
int ncap = cap * 2;
T* temp = new T[ncap];
for (int i = 0; i < cap; i++)
temp[i] = arr[i];
temp[cap] = obj;
delete [] arr;
arr = temp;
size++;
cap = ncap;
}
}
void pop_back()
{
size--;
if (size < (cap/2))
{
T* temp = new T[cap / 2];
for (int i = 0; i < size; i++)
temp[i] = arr[i];
delete[] arr;
arr = temp;
cap = cap / 2;
}
}
These two functions go into the public section of the vector class. There are some other functions as well that I have included in my vector class.
Vector() //default constructor
{
cap = 256; //using 256 capacity by default
arr = new T[cap];
size = 0;
}
Vector (int capacity) //parameterized constructor
{
arr = new T[capacity];
cap = capacity;
size = 0;
}
void reallocate(int capacity) //reallocation function
{
delete[] arr;
arr = nullptr;
arr = new T[capacity];
cap = capacity;
size = 0;
}
Vector(const Vector <T> & obj) //copy constructor
{
arr = new T[obj.cap];
cap = obj.cap;
for (int i = 0; i < obj.size; i++)
{
arr[i] = obj.arr[i];
}
size = obj.size;
}
const Vector<T> & operator = (const Vector <T> & obj) //assignment operator overload
{
if (arr != nullptr)
delete[] arr;
arr = new T[obj.cap];
cap = obj.cap;
for (int i = 0; i < obj.size; i++)
{
arr[i] = obj.arr[i];
}
size = obj.size;
return obj;
}
void insert (iterator & position, const T & value)
{
int index = 0;
for (Vector <int> ::iterator itr = begin(); itr != position; ++itr)
{
index++;
}
for (int i = size; i > index; i--)
{
arr[i] = arr[i-1];
}
//inserted
arr[index] = value;
size++;
if (size >= cap)
{
int ncap = cap * 2;
T* temp = new T[ncap];
for (int i = 0; i <= cap; i++)
temp[i] = arr[i];
arr = temp;
cap = ncap;
}
}
void erase (iterator & position)
{
int index = 0;
for (Vector <int> ::iterator itr = begin(); itr != position; ++itr)
{
index++;
}
for (int i = index; i < size-1; i++)
{
arr[i] = arr[i + 1];
}
size--;
if (size < (cap / 2))
{
T* temp = new T[cap / 2];
for (int i = 0; i < size; i++)
temp[i] = arr[i];
arr = temp;
cap = cap / 2;
}
}
What is an Iterator class?
Sometimes, we don't want to give public access to the class's private objects e.g. T* arr here through [ ] operator. In that case, we make another class within a class that provides us indirect access to that pointer/private object. The iterator for this vector class will look something like this:
class iterator {
protected:
T* ptr;
int i; //index
Public:
//other functions here
}
This class is in the public section of the Vector class.
I have also made some other function that assist using iterator.
iterator (T* aptr, int ind)
{
ptr = aptr;
i = ind;
}
void operator ++ ()
{
i++; //go to next index
}
const iterator & operator = (const iterator & obj)
{
if (arr != nullptr)
delete[] arr;
arr = new T[obj.cap];
cap = capacity;
for (int i = 0; i < obj.size; i++)
{
arr[i] = obj.arr[i];
}
size = obj.size;
return obj;
}
bool operator != (iterator & itr)
{
if (i != itr.i)
return true;
return false;
}
T & operator * ()
{
return ptr[i];
}
All of this goes in public section of the iterator class.
Now in the private section of the Vector class (after ending the iterator class), add these two functions.
iterator begin()
{
iterator itr(arr, 0);
return itr;
}
iterator end()
{
iterator itr(arr, size);
return itr;
}
The BEGIN function has return type Iterator. This function initializes the iterator (for whom this function is called) to the first index of the arr (arr is the dynamic array inside Vector class). Similarly, the END function has also the return type Iterator. This function initializes the iterator (for whom this function is called) to the last index of the arr (arr is the dynamic array inside Vector class).
This class is in the public section of the Vector class.
I have also made some other function that assist using iterator.
iterator (T* aptr, int ind)
{
ptr = aptr;
i = ind;
}
void operator ++ ()
{
i++; //go to next index
}
const iterator & operator = (const iterator & obj)
{
if (arr != nullptr)
delete[] arr;
arr = new T[obj.cap];
cap = capacity;
for (int i = 0; i < obj.size; i++)
{
arr[i] = obj.arr[i];
}
size = obj.size;
return obj;
}
bool operator != (iterator & itr)
{
if (i != itr.i)
return true;
return false;
}
T & operator * ()
{
return ptr[i];
}
All of this goes in public section of the iterator class.
Now in the private section of the Vector class (after ending the iterator class), add these two functions.
iterator begin()
{
iterator itr(arr, 0);
return itr;
}
iterator end()
{
iterator itr(arr, size);
return itr;
}
The BEGIN function has return type Iterator. This function initializes the iterator (for whom this function is called) to the first index of the arr (arr is the dynamic array inside Vector class). Similarly, the END function has also the return type Iterator. This function initializes the iterator (for whom this function is called) to the last index of the arr (arr is the dynamic array inside Vector class).
To illustrate more, let's see how we use the iterator class in our code/main.
Let's make a vector class object and use iterator to go through it.
Vector <int> v; //using 256 by default capacity
// int type
int s = 1;
for (int i = 0; i < 10; i++)
{
v.push_back(s++); //inserting values through push back function
}
Now let's make an iterator to be used in a for loop that displays the elements of the vector on the screen.
for ( Vector <int> ::iterator itr = v.begin() ; itr != v.end(); ++itr )
{
cout << *itr << " ";
}
cout << endl;
As you can see, we define an Iterator, named itr, using Vector <int> ::iterator itr and then we initialize it to the start of the vector using the BEGIN function in the vector as Vector <int> ::iterator itr = v.begin(). Similarly, we check that whether itr has reached the end of the vector using != operator and END function. And we move to the next index using ++ operator of the Iterator class.
Other than this, we can also insert or replace using iterator.
Vector <int> :: iterator itr = v.begin();
for (;*itr != 6; ++itr);
v.insert( itr , -1);
That's all for today. :)
Thank you for reading. 😊😎
0 comments:
Post a Comment