Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please convert this code to C + + template class: #pragma once #include class Vector { private: size _ t size; / / size _

Please convert this code to C++ template class:
#pragma once
#include
class Vector
{
private:
size_t size; // "size_t" largest number you can creat in mem
size_t capacity; //capacity is the size when you declare the array
int* arr; //pointer
public:
Vector() : arr(nullptr), size(0), capacity(0){}; //constructor
~Vector(){
delete[] arr; //how you delete an array
}//destructor
// paramaterized constructor
explicit Vector(size_t initialCapacity) : arr{ nullptr }, capacity{ initialCapacity }{//explicit== one constructor
arr = new int[capacity]; //created on the heap
}
size_t getSize() const {//const bc it wont change the state
return size;
}
bool isEmpty() const {
return(size ==0); //returns true if size==0
}
int& operator[](size_t index){//operator overloading
if (index >= size){
throw std::out_of_range("Index out of range.");
}
return arr[index];
}
//pushback function, you will have to create a new array because array size is set and copy, delete the old one, check if the array is full first
void push_back(int element){
if (size == capacity){
capacity =(capacity -0)?1 : 2* capacity; //if condition is true, return one, otherwise return 2* capacity
int* newArr = new int[capacity]; //capacity = new capacity
for (size_t i =0; i < size; i++){
newArr[i]= arr[i];
}
delete[]arr; //deletes old array
arr = newArr;
}
arr[size]= element; //if array isn't full, this just adds the new element to the array and the size increases
size++;
}
//insert function ie: insert(2,100), if your array is full, create another array and double the size
//copy elements before the wanted index and add the new element then insert the rest of the old ones
//or move all the old ones forward if the array isn't full
void insert(size_t index, int value){
if (size == capacity){//increases size if full
capacity =(capacity -0)?1 : 2* capacity; //if condition is true, return one, otherwise return 2* capacity
int* newArr = new int[capacity];
for (size_t i =0; i < index; i++){
newArr[i]= arr[i];
}
newArr[index]= value; //inserts value
for (size_t i = index; i < size; i++){
newArr[i +1]= arr[i];
}
delete[] arr;
arr = newArr;
size++;
}
else {//if array isnt full, shifts elements after index
for (size_t i =0; i > index; --i){
arr[i]= arr[i -1];
}
arr[index]= value;
size++;
}
}
//popback
void popBack(){
if (size >0){
size--; //removes last element and decreases size
}
}
//erases an element at a specific position
//create a new array and exclude the index of the value
//delete old array
void erase(size_t index){
int* newArr;
for (size_t i =0; i < size; i++){//goes through the whole array
if (i != arr[index])//if not equal to given index, copies it to the new array
newArr[i]= arr[i];
}
delete[] arr; //deletes old array
arr = newArr; //sets new array to array
size--; //decreases size
}
//operator[]: get the element at a specific index
int getIndexElement(size_t index){
return arr[index];
}
//return size of array--- already done at line 24
};

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago