Answered step by step
Verified Expert Solution
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:
sizet size; "sizet largest number you can creat in mem
sizet capacity; capacity is the size when you declare the array
int arr; pointer
public:
Vector : arrnullptr size capacity; constructor
~Vector
delete arr; how you delete an array
destructor
paramaterized constructor
explicit Vectorsizet initialCapacity : arr nullptr capacity initialCapacity explicit one constructor
arr new intcapacity; created on the heap
sizet getSize const const bc it wont change the state
return size;
bool isEmpty const
returnsize ; returns true if size
int& operatorsizet indexoperator overloading
if index size
throw std::outofrangeIndex out of range.";
return arrindex;
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 pushbackint element
if size capacity
capacity capacity : capacity; if condition is true, return one, otherwise return capacity
int newArr new intcapacity; capacity new capacity
for sizet i ; i size; i
newArri arri;
deletearr; deletes old array
arr newArr;
arrsize element; if array isn't full, this just adds the new element to the array and the size increases
size;
insert function ie: insert 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 insertsizet index, int value
if size capacityincreases size if full
capacity capacity : capacity; if condition is true, return one, otherwise return capacity
int newArr new intcapacity;
for sizet i ; i index; i
newArri arri;
newArrindex value; inserts value
for sizet i index; i size; i
newArri arri;
delete arr;
arr newArr;
size;
else if array isnt full, shifts elements after index
for sizet i ; i index; i
arri arri ;
arrindex value;
size;
popback
void popBack
if size
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 erasesizet index
int newArr;
for sizet i ; i size; igoes through the whole array
if i arrindexif not equal to given index, copies it to the new array
newArri arri;
delete arr; deletes old array
arr newArr; sets new array to array
size; decreases size
operator: get the element at a specific index
int getIndexElementsizet index
return arrindex;
return size of array already done at line
;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started