As introduced in the lecture notes, the vector class is C++\'s answer to the lim
ID: 666827 • Letter: A
Question
As introduced in the lecture notes, the vector class is C++'s answer to the limitations of arrays. As compared with arrays, vectors protect the user from accessing out-of-bounds ranges, they resize dynamically on demand, and they know and can report their length.
This assignment will test your ability to create a "class library." This means that, while you may create a main function for testing your own code, the main functionmay not be submitted. Your code will be tested against a different main function. As a result, it is very important that you match class and function names to what we specify.
Your class must be named myvector and take one template type argument. This type argument will specify the type of data stored in the vector, such as a vector of ints, doubles, etc. Your class must have at least the following public members, which shall function as described:
int size() const; // return the current length of the vector
int capacity() const; // return the current capacity of the vector
bool empty() const; // return whether or not the vector is empty
void resize(int); // resize the vector (change length) to the specified size, changing capacity as needed
void reserve(int); // change the vector capacity to the specified size, or give an error if capacity would get smaller than length
void assign(int, T const &); // assign the second argument as the value of the vector at the position of the first argument
void push_back(T const &); // increase length by 1 and add the argument as the value at the end
T pop_back(); // decrease length by 1, returning the value removed from the vector
void insert(int, T const &); // increase length by 1, put the value specified in the second argument at index specified by first argument and shift all values to the right in the vector down by one index
void erase(int); // decrease length by 1, remove the item at the specified index, and shift all other items left to eliminate the "hole"
void erase(int, int); // same as erase(int), but removes all elements between the indexes specified by the first and second argument
void clear(); // remove all elements from the list
You must also implement the [ ] (index) operator, which should return a member of the vector at the index specified. An error should be thrown if the index is out of range. A function named at should also exist and perform the exact same functionality. Thus a user of your class should be able to do:
myvector<int> x;
x.resize(2);
x[0] = 10;
and also should be able to do:
myvector<int> x;
x.push_back(5);
myvector<int> const y = x;
int const z = y[0];
You also need constructors that take the following arguments:
int -- initial capacity of the vector (length will be zero)
another vector of the same type (as a const reference) -- copy the contents of the vector
an array of the same type and the integer array length -- copy contents of array into the vector
This class should be "well-defined," so you will need copy and move constructors, assignment operator overloads, etc. in order to make the class work properly.
To clarify the behavior of a vector: The vector has a "logical" size and a "physical"capacity. The vector over-allocates RAM in its internal physical array in order to make it efficient to resize the vector without having to create a new internal physical array each time. The length of the physical array is the capacity. The length we pretend that the vector is limited to when presenting a public facade to the outside world is the logical size. Resizing the vector should only change capacity when it needs to in order to make additional room for the new size. Resizing the vector to a smaller size should never (at least in our simple myvector class) reclaim capacity. Thus the vector is "grow-only."
When resizing the internal array to store more than the existing capacity can hold, you must use a reasonable algorithm to ensure that enough "buffer" space is left at the end of the vector so that every operation that adds more to the vector does not require a subsequent resize of the internal array. Doubling the length of the internal array (or setting it to be equal to the new required capacity, whichever is larger) is a reasonable algorithm. Adding n+1, where n is the required capacity, is not a reasonable algorithm since nearly every subsequent operation would require changing the internal array length.
You can detecting all conditions where range is exceeded, and throwing anout_of_range exception rather than crashing.
Explanation / Answer
I write some code not exactly but it will definitly help you to solve the problem by understanding it and the code is as given below: -
and
/ / objects of type class X
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.