180 lines
3.7 KiB
C++
180 lines
3.7 KiB
C++
#pragma once
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
class MyVector
|
|
{
|
|
private:
|
|
int* vector = nullptr;
|
|
int currentIndex = 0; //add new element at this index
|
|
int maxCapacity = 13;
|
|
int maxConstValue = 3;
|
|
int currentCapacity = 0;
|
|
public:
|
|
|
|
MyVector() {
|
|
this->vector = new int[currentCapacity];
|
|
};
|
|
MyVector(int capacity) {
|
|
try
|
|
{
|
|
if (capacity > maxCapacity)
|
|
{
|
|
this->currentCapacity = this->maxCapacity;
|
|
this->vector = new int[maxCapacity];
|
|
throw std::invalid_argument(
|
|
"Unable To Create MyVector With Given Capacity Of " + \
|
|
std::to_string(capacity) + \
|
|
". Max Capacity Of MyVector is " + \
|
|
std::to_string(this->maxCapacity) +\
|
|
" Elements.\nMyVector With Max Capacity Of "+\
|
|
std::to_string(this->maxCapacity) + \
|
|
" Has Been Created."
|
|
);
|
|
}
|
|
else {
|
|
this->currentCapacity = capacity;
|
|
this->vector = new int[capacity];
|
|
}
|
|
}
|
|
catch (const std::exception& e)
|
|
{
|
|
std::cout << e.what() << "\n";
|
|
}
|
|
};
|
|
MyVector(const MyVector& obj) {
|
|
this->vector = obj.vector;
|
|
this->currentIndex = obj.currentIndex;
|
|
this->currentCapacity = obj.currentCapacity;
|
|
};
|
|
~MyVector() {};
|
|
|
|
int getLength() {
|
|
return this->currentIndex;
|
|
};
|
|
|
|
int operator()() {
|
|
return this->currentIndex;
|
|
};
|
|
|
|
int operator[](int index) {
|
|
try
|
|
{
|
|
if (index > (this->currentIndex-1) || index < 0)
|
|
{
|
|
throw std::invalid_argument("Index out of vector range");
|
|
return NULL;
|
|
}
|
|
else {
|
|
return vector[index];
|
|
}
|
|
}
|
|
catch (const std::exception& e)
|
|
{
|
|
std::cout << e.what() << "\n";
|
|
}
|
|
}
|
|
|
|
void operator + (int value) {
|
|
try
|
|
{
|
|
if (value > this->maxConstValue)
|
|
{
|
|
throw std::invalid_argument(
|
|
"Unable to Increase Elements. Given Number " + \
|
|
std::to_string(value) + \
|
|
" Is Beyond The Limit. Current Limit Is " +\
|
|
std::to_string(this->maxConstValue) +\
|
|
"."
|
|
);
|
|
}
|
|
else {
|
|
for (int i = 0; i <= this->currentIndex; i++)
|
|
{
|
|
this->vector[i] += value;
|
|
};
|
|
}
|
|
}
|
|
catch (const std::exception& e)
|
|
{
|
|
std::cout << e.what() << "\n";
|
|
}
|
|
};
|
|
|
|
void operator - (int value) {
|
|
try
|
|
{
|
|
if (this->currentIndex == 0)
|
|
{
|
|
throw std::invalid_argument(
|
|
"Unable to Remove Elements. MyVector Is Empty."
|
|
);
|
|
}
|
|
else if (value > this->currentIndex) {
|
|
throw std::invalid_argument(
|
|
"Unable to Remove Elements. Given Value Of " + \
|
|
std::to_string(value) + \
|
|
" More Than Number Of Elements In MyVector Which Is " + \
|
|
std::to_string(this->currentIndex) + \
|
|
"."
|
|
);
|
|
}
|
|
else if (value == this->currentIndex) {
|
|
delete[] this->vector;
|
|
this->vector = new int[0];
|
|
this->currentIndex = 0;
|
|
}
|
|
else {
|
|
int* tmp = new int[this->currentIndex - value];
|
|
|
|
for (int i = 0; i < this->currentIndex - value; i++)
|
|
{
|
|
tmp[i] = this->vector[i];
|
|
};
|
|
this->currentIndex -= value;
|
|
|
|
delete[] this->vector;
|
|
this->vector = tmp;
|
|
}
|
|
}
|
|
catch (const std::exception& e)
|
|
{
|
|
std::cout << e.what() << "\n";
|
|
}
|
|
};
|
|
|
|
void push_back(int value) {
|
|
try
|
|
{
|
|
if (this->currentIndex == this->maxCapacity) {
|
|
throw std::invalid_argument("Unable To Add Element. MyVector Has Reached Capacity Limit.\n");
|
|
}
|
|
else {
|
|
//if vector capacity reached, increase it size
|
|
if (this->currentIndex == this->currentCapacity)
|
|
{
|
|
int* tmp = new int[this->currentCapacity + 1];
|
|
|
|
for (int i = 0; i < this->currentIndex; i++)
|
|
{
|
|
tmp[i] = this->vector[i];
|
|
};
|
|
tmp[currentIndex] = value;
|
|
this->currentIndex++;
|
|
this->currentCapacity++;
|
|
|
|
delete[] this->vector;
|
|
this->vector = tmp;
|
|
}
|
|
else {
|
|
this->vector[currentIndex] = value;
|
|
this->currentIndex++;
|
|
}
|
|
}
|
|
}
|
|
catch (const std::exception& e)
|
|
{
|
|
std::cout << e.what() << "\n";
|
|
}
|
|
};
|
|
}; |