Question
FINISH THE TODOS FOR THE .H FILE Most of it is done, just the ending need a little help on. #ifndef VECTOR_H #define VECTOR_H #include
FINISH THE TODOS FOR THE .H FILE
Most of it is done, just the ending need a little help on.
#ifndef VECTOR_H
#define VECTOR_H
#include
#include
#include
#include
template
class Vector {
public:
class iterator;
private:
T* array;
size_t _capacity, _size;
// You may want to write a function that grows the vector
void grow() {
_capacity = (_capacity == 0) ? 1 : 2 * _capacity;
T* temp = new T[_capacity];
std::copy(array, array + _size, temp);
delete[] array;
array = temp;
}
public:
Vector() noexcept : array(nullptr), _capacity(0), _size(0) {}
Vector(size_t count, const T& value): array(new T[count]), _capacity(count), _size(count) {std::fill(array, array + count, value);}
explicit Vector(size_t count) : array(new T[count]), _capacity(count), _size(count) {}
Vector(const Vector& other) : array(new T[other._size]), _capacity(other._size), _size(other._size) { std::copy(other.array, other.array + other._size, array); }
Vector(Vector&& other) noexcept : array(other.array),
_capacity(other._capacity),
_size(other._size) {
other.array = nullptr;
other._capacity = other._size = 0;
}
~Vector() {delete[] array;}
Vector& operator=(const Vector& other) {
if (this != &other) {
T*temp = new T[other._size]; std::copy(other.array, other.array + other._size, temp); delete[] array; array = temp;
_capacity = other._capacity;
_size = other._size;
}
return *this;
}
Vector& operator=(Vector&& other) noexcept {
if (this != &other) {
delete[] array; array = other.array; _capacity = other._capacity; _size = other._size; other.array = nullptr; other._capacity = other._size = 0;
}
return *this;
}
iterator begin() noexcept {return iterator(array); }
iterator end() noexcept { return iterator(array + _size); }
[[nodiscard]] bool empty() const noexcept { return _size == 0;}
size_t size() const noexcept { return _size; }
size_t capacity() const noexcept { return _capacity; }
T& at(size_t pos) {
if (pos >= _size) throw std::out_of_range("out of range");
return array[pos];
}
const T& at(size_t pos) const {
if (pos >= _size) {
throw std::out_of_range("Vector::at");
}
return array[pos];
}
T& operator[](size_t pos) {return array[pos];}
const T& operator[](size_t pos) const {return array[pos];}
T& front() {return array[0];}
const T& front() const {return array[0];}
T& back() {return array[_size - 1];}
const T& back() const {return array[_size - 1];}
void push_back(const T& value) { if (_size == _capacity) grow(); array[_size++] = value; }
void push_back(T&& value) { if (_size == _capacity) grow(); array[_size++] = std::move(value);}
void pop_back() {--_size;}
iterator insert(iterator pos, const T& value) {
size_t index = pos - begin();
if (_size == _capacity) grow();
for (size_t i = _size; i > index; --i)
array[i] = std::move(array[i - 1]);
array[index] = value;
++_size;
return begin() + index;
}
iterator insert(iterator pos, T&& value) { /* TODO */ }
iterator insert(iterator pos, size_t count, const T& value) { /* TODO */ }
iterator erase(iterator pos) { /* TODO */ }
iterator erase(iterator first, iterator last) { /* TODO */ }
class iterator {
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = T;
using difference_type = ptrdiff_t;
using pointer = T*;
using reference = T&;
private:
// Add your own data members here
// HINT: For random_access_iterator, the data member is a pointer 99.9% of the time
public:
iterator() { /* TODO */ }
// Add any constructors that you may need
// This assignment operator is done for you, please do not add more
iterator& operator=(const iterator&) noexcept = default;
[[nodiscard]] reference operator*() const noexcept { /* TODO */ }
[[nodiscard]] pointer operator->() const noexcept { /* TODO */ }
// Prefix Increment: ++a
iterator& operator++() noexcept { /* TODO */ }
// Postfix Increment: a++
iterator operator++(int) noexcept { /* TODO */ }
// Prefix Decrement: --a
iterator& operator--() noexcept { /* TODO */ }
// Postfix Decrement: a--
iterator operator--(int) noexcept { /* TODO */ }
iterator& operator+=(difference_type offset) noexcept { /* TODO */ }
[[nodiscard]] iterator operator+(difference_type offset) const noexcept { /* TODO */ }
iterator& operator-=(difference_type offset) noexcept { /* TODO */ }
[[nodiscard]] iterator operator-(difference_type offset) const noexcept { /* TODO */ }
[[nodiscard]] difference_type operator-(const iterator& rhs) const noexcept { /* TODO */ }
[[nodiscard]] reference operator[](difference_type offset) const noexcept { /* TODO */ }
[[nodiscard]] bool operator==(const iterator& rhs) const noexcept { /* TODO */ }
[[nodiscard]] bool operator!=(const iterator& rhs) const noexcept { /* TODO */ }
[[nodiscard]] bool operator<(const iterator& rhs) const noexcept { /* TODO */ }
[[nodiscard]] bool operator>(const iterator& rhs) const noexcept { /* TODO */ }
[[nodiscard]] bool operator<=(const iterator& rhs) const noexcept { /* TODO */ }
[[nodiscard]] bool operator>=(const iterator& rhs) const noexcept { /* TODO */ }
};
void clear() noexcept { /* TODO */ }
};
// This ensures at compile time that the deduced argument _Iterator is a Vector
// There is no way we know of to back-substitute template
// because it leads to a non-deduced context
namespace {
template
using is_vector_iterator = std::is_same
}
template
[[nodiscard]] _Iterator operator+(typename _Iterator::difference_type offset, _Iterator const& iterator) noexcept { /* TODO */ }
#endif
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