Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

So i have this .h file thats pretty much done but I'm having some issues with it that I need reviewd #ifndef VECTOR_H #define VECTOR_H

So i have this .h file thats pretty much done but I'm having some issues with it that I need reviewd

#ifndef VECTOR_H

#define VECTOR_H

#include // std::random_access_iterator_tag

#include // size_t

#include // std::out_of_range

#include // std::is_same

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) {

if (_size == _capacity) grow();

size_t index = pos - begin();

for (size_t i = _size; i > index; --i) {

array[i] = array[i - 1];

}

array[index] = value;

++_size;

return iterator(array + index);

}

iterator insert(iterator pos, T&& value) {

if (_size == _capacity) grow();

size_t index = pos - begin();

for (size_t i = _size; i > index; --i) {

array[i] = array[i - 1];

}

array[index] = std::move(value);

++_size;

return iterator(array +index);

}

iterator insert(iterator pos, size_t count, const T& value) {

if (_size + count > _capacity) {

_capacity = _size + count;

T* temp = new T[_capacity];

std::copy(array, array + _size, temp);

delete[] array;

array = temp;

}

size_t index = pos - begin();

for (size_t i = _size + count - 1; i >= index + count; --i) {

array[i] = array[i - count];

}

for (size_t i = index; i

array[i] = value;

}

_size += count;

return iterator(array + index);

}

iterator erase(iterator pos) {

size_t index = pos - begin();

for (size_t i = index; i

array[i] = array[i + 1];

}

--_size;

return iterator(array + index);

}

iterator erase(iterator first, iterator last) {

size_t index1 = first - begin();

size_t index2 = last - begin();

for (size_t i = index1; i

array[i] = array[i + (index2 - index1)];

}

_size -= (index2 - index1);

return iterator(array + index1);

}

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

[[nodiscard]] bool operator>(const iterator& rhs) const noexcept {return ptr > rhs.ptr; }

[[nodiscard]] bool operator

[[nodiscard]] bool operator>=(const iterator& rhs) const noexcept {return ptr >= rhs.ptr; }

};

void clear() noexcept { /* TODO */ } // I do need a little help understanding this line

};

// This ensures at compile time that the deduced argument _Iterator is a Vector::iterator

// There is no way we know of to back-substitute template for external functions

// because it leads to a non-deduced context

namespace {

template

using is_vector_iterator = std::is_same::iterator, _Iterator>;

}

template ::value>

[[nodiscard]] _Iterator operator+(typename _Iterator::difference_type offset, _Iterator const& iterator) noexcept { /* TODO */ } // this line too

#endif

Everytime I submit it, i get these errors, can anyone explain what they mean. Also I bolded 2 lines that are confusing, could someome explain what to do for them

image text in transcribed

STDOUT \& STDERR ----CMD: 0---- ---sTDOUT--- make: Entering directory '/autograder/source/tests' rm rtest/utils/memhook.o rtest/utils/xoshiro256.o rtest/utils/typegen.o rtest/utils/assertions.o make: Leaving directory '/autograder/source/tests' ----STDERR---- In file included from include/executable. h:14, from tests/vector_begin.cpp:1: ../../submission/Vector.h; In instantiation of 'Vector : :iterator Vector: :begin() [with T= int]': tests/vector_begin.cpp:17:45: required from here ../../submission/vector.h:55:39: error: no matching function for call to 'vector: :iterator::iterator(int*\&)' 55 | iterator begin( ) noexcept (return iterator (array); } ../../submission/vector.ht152:9t notet candidatet 'Vectort:iteratorttiterator() [With T - int]' 152 | iterator (\} (ptr = nullptr; } ../../submission/vector.h:152:9 note + candidate expects 0 arguments, 1 provided ../../submission/vector.ht140:11: note: candidate: 'constexpr vector: :iteratort:iterator(const Vectort:iterator )' 140 | class iterator f ../../submission/vector.h 140:11t note: no known conversion for argument 1 from 'int*' to 'const vector tt iteratork' make: *** [rtest/makefile:106: build/vector_begin] Error 1 Test exited with 2 on cmd "make -C tests RTEST_SRC_DIR=../../submission run/vector_begin

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions