Question
COMPLETE THE.H FILE DONE MOST OF IT, JUST NEED HELP WITH THE BOLDED TODOS #ifndef VECTOR_H #define VECTOR_H #include // std::random_access_iterator_tag #include // size_t #include
COMPLETE THE.H FILE
DONE MOST OF IT, JUST NEED HELP WITH THE BOLDED TODOS
#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) { /* TODO */ }
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
T* ptr;
public:
iterator() {ptr = nullptr;}
// 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 {return *ptr;}
[[nodiscard]] pointer operator->() const noexcept { return ptr; }
// Prefix Increment: ++a
iterator& operator++() noexcept {ptr++; return *this;}
// Postfix Increment: a++
iterator operator++(int) noexcept {T* temp = ptr;++(ptr); return temp;}
// Prefix Decrement: --a
iterator& operator--() noexcept {ptr--; return *this;}
// Postfix Decrement: a--
iterator operator--(int) noexcept { T* temp = ptr; --(ptr); return temp;}
iterator& operator+=(difference_type offset) noexcept {ptr += offset; return *this;}
[[nodiscard]] iterator operator+(difference_type offset) const noexcept {T* temp = ptr; temp = temp + offset; return temp; }
iterator& operator-=(difference_type offset) noexcept { ptr -= offset; return *this; }
[[nodiscard]] iterator operator-(difference_type offset) const noexcept {T* temp = ptr; temp = temp - offset; return temp;}
[[nodiscard]] difference_type operator-(const iterator& rhs) const noexcept {return ptr - rhs.ptr;}
[[nodiscard]] reference operator[](difference_type offset) const noexcept {return *(ptr+offset);}
[[nodiscard]] bool operator==(const iterator& rhs) const noexcept {return ptr == rhs.ptr;}
[[nodiscard]] bool operator!=(const iterator& rhs) const noexcept {return ptr != rhs.ptr; }
[[nodiscard]] bool operator<(const iterator& rhs) const noexcept {return ptr < rhs.ptr; }
[[nodiscard]] bool operator>(const iterator& rhs) const noexcept {return ptr > rhs.ptr; }
[[nodiscard]] bool operator<=(const iterator& rhs) const noexcept { return ptr <= rhs.ptr; }
[[nodiscard]] bool operator>=(const iterator& rhs) const noexcept {return ptr >= rhs.ptr; }
};
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