Question
Background You have been using std::vector for a while now. In this assignment you will implement a simple version of vector, that just holds ints.
Background You have been using std::vector for a while now. In this assignment you will implement a simple version of vector, that just holds ints. You will define some of the same member functions that std::vector does, and they will behave the same way. The goal of this project is to learn how to manage dynamic memory on the heap. You will allocate an array on the heap for your Vector class to store ints in. When your underlying memory is used up, and you want to append or insert a new element, you will need to allocate a new array, copy the current elements to it, and then delete the old array.
Requirements Call your class Vector (capital-v). Your constructor should allocate an initial array big enough to hold 10 ints (a constant named CHUNK), which is its initial capacity", but the "size" (the numbers of elements currently in use) will initially be zero. Do not hard-code the 10. (See the header below). When you expand the array, make the new capacity 1.6 times the current capacity (initially CHUNK). The following diagram illustrates a Vector instance managing heap memory.
I am providing the Vector.h file for you that declares everything in the class. Your job is to implement the member functions in the file Vector.cpp. The file Vector.h is found in the Canvas assignment, along with a test driver for it.
using std::size_t: class Vector enum (CHUNK - 10); int data_ptr; // Pointer to the heap array size_t capa">
using std::size_t: class Vector enum (CHUNK - 10); int data_ptr; // Pointer to the heap array size_t capa">
Vector.h code
#ifndef VECTOR_H #define VECTOR_H
#include using std::size_t;
class Vector { enum {CHUNK = 10}; int* data_ptr; // Pointer to the heap array size_t capacity; // Size of the current array allocation (total number of ints, in use or not) size_t n_elems; // Number of int spaces currently in use, starting from position 0 void grow(); public: // Object Mgt. Vector(); Vector(const Vector& v); // Copy constructor Vector& operator=(const Vector& v); // Copy assignment operator ~Vector();
// Accessors int front() const; // Return the int in position 0, if any int back() const; // Return last element (position n_elems-1) int at(size_t pos) const; // Return element in position "pos" (0-based) size_t size() const; // Return n_elems bool empty() const; // Return n_elems == 0
// Mutators int& operator[](size_t pos); // Same as at but no bounds checking void push_back(int item); // Append a new element at the end of the array void pop_back(); // --n_elems (nothing else to do; returns nothing) void erase(size_t pos); // Remove item in position pos and shuffles following items left void insert(size_t pos, int item); // Shuffle items right to make room for a new element void clear(); // n_elems = 0 (nothing else to do; keep the current capacity)
// Iterators int* begin(); // Return a pointer to 1st element, or nullptr if n_elems == 0 int* end(); // Return a pointer to 1 past last element, or nullptr if n_elems == 0
// Comparators bool operator==(const Vector& v) const; bool operator!=(const Vector& v) const; };
#endif
------------------------------------------------------------------------------------------------------------------------------------------------------------
Test.h code
#ifndef TEST_H #define TEST_H #include
// Unit Test Scaffolding: Users call test_, fail_, succeed_, throw_, nothrow_, and report_ // AUTHOR: Chuck Allison (Creative Commons License, 2001 - 2017)
namespace { int nPass{0}; int nFail{0}; void do_fail(const char* text, const char* fileName, long lineNumber) { std::cout ++nFail; } void do_test(const char* condText, bool cond, const char* fileName, long lineNumber) { if (!cond) do_fail(condText, fileName, lineNumber); else ++nPass; } void succeed_() { ++nPass; } void report_() { std::cout std::cout std::cout } }
#define test_(cond) do_test(#cond, cond, __FILE__, __LINE__) #define fail_(text) do_fail(text, __FILE__, __LINE__) #define throw_(text,T) \ try { \ text; \ std::cout do_fail(#text,__FILE__,__LINE__); \ } catch (T&) { \ ++nPass; \ } catch (...) { \ std::cout do_fail(#text,__FILE__,__LINE__); \ }
#define nothrow_(text) \ try { \ text; \ ++nPass; \ } catch (...) { \ std::cout do_fail(#text,__FILE__,__LINE__); \ } #endif
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
testVector.cpp code
#include "Vector.h" #include "test.h" #include using namespace std;
int main() { // Test exceptions Vector v; throw_(v.at(0), range_error); throw_(v.pop_back(), range_error); throw_(v.erase(0), range_error); throw_(v.front(), range_error); throw_(v.back(), range_error);
// Test adding an element v.push_back(1); test_(v.size() == 1); test_(v.at(0) == 1); test_(v[0] == 1); test_(v.front() == 1); test_(v.back() == 1); test_(!v.empty());
// Add another v.push_back(2); test_(v.size() == 2); test_(v.at(0) == 1); test_(v.at(1) == 2); test_(v[0] == 1); test_(v[1] == 2); test_(v.front() == 1); test_(v.back() == 2); test_(!v.empty());
// Test iterators auto iter = v.begin(); test_(*iter == 1); ++iter; test_(*iter == 2); ++iter; test_(iter == v.end());
// Test copy and == Vector v2 = v; test_(v2.size() == 2); test_(v2.at(0) == 1); test_(v2.at(1) == 2); test_(v2[0] == 1); test_(v2[1] == 2); test_(v2.front() == 1); test_(v2.back() == 2); test_(!v2.empty()); test_(v == v2);
iter = v2.begin(); test_(*iter == 1); ++iter; test_(*iter == 2); ++iter; test_(iter == v2.end());
// Test assignment Vector v3; v3 = v; test_(v3.size() == 2); test_(v3.at(0) == 1); test_(v3.at(1) == 2); test_(v3[0] == 1); test_(v3[1] == 2); test_(v3.front() == 1); test_(v3.back() == 2); test_(!v3.empty());
iter = v3.begin(); test_(*iter == 1); ++iter; test_(*iter == 2); ++iter; test_(iter == v3.end());
// Test assignment v[1] = -2; test_(v.back() == -2); test_(v.at(1) == -2); test_(v[1] == -2);
// Test pop_back v.pop_back(); test_(v.size() == 1); test_(v.front() == 1); test_(v.back() == 1); test_(v.at(0) == 1); test_(v[0] == 1);
// Test clear and != v.clear(); test_(v.size() == 0); test_(v.empty()); throw_(v.at(0), range_error); throw_(v.pop_back(), range_error); throw_(v.erase(0), range_error); throw_(v.front(), range_error); throw_(v.back(), range_error); test_(v != v2);
// Test erase v3.erase(0); test_(v3.size() == 1); test_(v3.at(0) == 2); test_(v3[0] == 2); test_(v3.front() == 2); test_(v3.back() == 2);
// Test insert v3.insert(0,1); test_(v3.size() == 2); test_(v3.at(0) == 1); test_(v3[0] == 1); test_(v3[1] == 2); test_(v3.front() == 1); test_(v3.back() == 2);
// Test grow Vector v4; for (int i = 1; i => v4.push_back(i); test_(v4.size() == 10); test_(v4.front() == 1); test_(v4.back() == 10); v4.insert(10,11); test_(v4.size() == 11); test_(v4.front() == 1); test_(v4.back() == 11);
report_(); }
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