Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //change the capacity
- void Vector::reserve(int n)
- {
- int num;
- if (this->_capacity < n)
- {
- num = ((n - this->_capacity) / this->_resizeFactor) + 1;
- this->make_more_space(num);
- }
- }
- //change the size
- void Vector::resize(int n)
- {
- int i;
- if (this->_capacity >= n)
- {
- for (i = this->_capacity; i >= n; i--)
- {
- this->_elements[i] = 0;
- this->_size -= 1;
- }
- }
- else
- {
- this->reserve(n);
- }
- }
- //assigns val to all elemnts
- void Vector::assign(int val)
- {
- int i;
- for (i = 0; i < this->_size -1; i++)
- {
- this->_elements[i] = val;
- }
- }
- //change the size
- void Vector::resize(int n, const int& val)
- {
- int i;
- if (this->_capacity >= n)
- {
- for (i = this->_capacity; i >= n; i--)
- {
- this->_elements[i] = 0;
- this->_size -= 1;
- }
- }
- else
- {
- this->reserve(n);
- }
- this->assign(val);
- }
- Vector::Vector(const Vector& other)
- {
- this->_elements = new int(*(other._elements));
- this->_size = other._size;
- this->_capacity = other._capacity;
- this->_resizeFactor = other._resizeFactor;
- }
- Vector& Vector::operator=(const Vector& other)
- {
- delete[]this->_elements;
- this->_elements = new int(*(other._elements));
- this->_size = other._size;
- this->_capacity = other._capacity;
- this->_resizeFactor = other._resizeFactor;
- return *this;
- }
- int& Vector::operator[](int n) const
- {
- if (n >= this->_size)
- {
- std::cout << "n bigger then elements size" << std::endl;
- return (this->_elements[0]);
- }
- else
- {
- return(this->_elements[n]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement