Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class myVector
- {
- private:
- float *arr;
- int max_size;
- int size;
- int resizeCoeff; // add/remove resizeCoeff elements
- void resize(int option); // option -1 -> decrease, 1 -> increase
- public:
- myVector();
- myVector(int max_size, int resize_coeff);
- myVector(float *a, int n);
- myVector(myVector *other);
- ~myVector();
- int count();
- int capacity();
- void insert(int pos, float el);
- void push_front(float el);
- void push_back(float el);
- float remove(int pos);
- float remove_front();
- float remove_back();
- float at(int index);
- };
- void myVector::resize(int option)
- {
- this->max_size += (option * this->resizeCoeff);
- float *newArr = new float[this->max_size];
- for (int i = 0; i < this->size; i++)
- {
- newArr[i] = this->arr[i];
- }
- delete[] arr;
- this->arr = newArr;
- }
- myVector::myVector()
- {
- this->arr = new float[10];
- this->max_size = 10;
- this->size = 0;
- this->resizeCoeff = 5;
- }
- myVector::myVector(int max_size, int resize_coeff)
- {
- this->arr = new float[max_size];
- this->max_size = max_size;
- this->size = 0;
- this->resizeCoeff = resize_coeff;
- }
- myVector::myVector(float *a, int n)
- {
- this->arr = new float[n + 10];
- for (int i = 0; i < n; i++)
- {
- this->arr[i] = a[i];
- }
- this->max_size = n + 10;
- this->size = n;
- }
- myVector::myVector(myVector *other)
- {
- this->arr = new float[other->capacity()];
- for (int i = 0; i < other->count(); i++)
- {
- this->arr[i] = other->at(i);
- }
- this->max_size = other->capacity();
- this->size = other->count();
- }
- myVector::~myVector()
- {
- delete[] arr;
- }
- int myVector::count()
- {
- return this->size;
- }
- int myVector::capacity()
- {
- return this->max_size;
- }
- void myVector::push_back(float el)
- {
- insert(this->size, el);
- }
- void myVector::insert(int pos, float el)
- {
- if (this->size == this->max_size)
- {
- this->resize(1);
- }
- for (int i = size; i > pos; i--)
- {
- this->arr[i] = this->arr[i-1];
- }
- this->arr[pos] = el;
- this->size++;
- }
- void myVector::push_front(float el)
- {
- this->insert(0, el);
- }
- float myVector::remove(int pos){
- int el = this->arr[pos];
- for (int i = pos; i < this->size - 1; i++)
- {
- this->arr[i] = this->arr[i+1];
- }
- this->size--;
- if (this->size + this->resizeCoeff < this->max_size)
- {
- this->resize(-1);
- }
- return el;
- }
- float myVector::remove_front()
- {
- return remove(0);
- }
- float myVector::remove_back()
- {
- return remove(this->size - 1);
- }
- float myVector::at(int index)
- {
- if (index < 0 || index >= this->size)
- {
- throw (index);
- }
- return this->arr[index];
- }
- int main()
- {
- myVector v1;
- for(int i = 0; i <= 11; i++)
- {
- v1.push_back(i);
- }
- v1.insert(5, 19);
- v1.remove(3);
- v1.remove_front();
- v1.push_front(31);
- v1.remove_back();
- cout << "V1: Capacity: " << v1.capacity() << " Count: " << v1.count();
- cout << " Elements: ";
- for(int i = 0; i < v1.count(); i++)
- {
- cout << v1.at(i) << " ";
- }
- cout << endl;
- float nums[] = {10, 20, 30, 40, 50};
- myVector v2(nums, 5);
- cout << "V2: Capacity: " << v2.capacity() << " Count: " << v2.count();
- cout << " Elements: ";
- for(int i = 0; i < v2.count(); i++)
- {
- cout << v2.at(i) << " ";
- }
- cout << endl;
- myVector v3(&v2);
- cout << "V3: Capacity: " << v3.capacity() << " Count: " << v3.count();
- cout << " Elements: ";
- for(int i = 0; i < v3.count(); i++)
- {
- cout << v3.at(i) << " ";
- }
- cout << endl;
- myVector v4(20, 10);
- for(int i = 1; i <= 21; i++){
- v4.push_back(i);
- }
- cout << "V4: Capacity: " << v4.capacity() << " Count: " << v4.count();
- cout << " Elements: ";
- for(int i = 0; i < v4.count(); i++)
- {
- cout << v4.at(i) << " ";
- }
- cout << endl;
- try
- {
- float num = v1.at(-19);
- cout << num; // not print
- }
- catch (int index)
- {
- cout << index << " is invalid index!" << endl;
- }
- try
- {
- float num = v1.at(23);
- cout << num; // not print
- }
- catch (int index)
- {
- cout << index << " is invalid index!" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement