Question: #ifndef ARRAY_H #define ARRAY_H /************************************************************/ // System includes #include #include #include #include /************************************************************/ // Local includes /************************************************************/ // Using declarations using std::copy; using std::copy_backward; using
#ifndef ARRAY_H
#define ARRAY_H
/************************************************************/
// System includes
#include
#include
#include
#include
/************************************************************/
// Local includes
/************************************************************/
// Using declarations
using std::copy;
using std::copy_backward;
using std::distance;
using std::fill;
using std::ostream;
using std::ptrdiff_t;
/************************************************************/
template
class Array
{
public:
//*****************************************************
// DO NOT MODIFY THIS SECTION!
// Some standard Container type aliases
using value_type = T;
// Iterators are just pointers to objects of type T
using iterator = value_type*;
using const_iterator = const value_type*;
using reference = value_type&;
using const_reference = const value_type&;
using size_type = size_t;
using difference_type = ptrdiff_t;
//*****************************************************
// Default ctor.
// Initialize an empty Array.
// This method is complete, and does NOT need modification.
// Remember to use a _member initialization list_ for each ctor,
// like I have below for the default ctor.
Array ()
: m_size (0),
m_capacity (0),
m_array (nullptr)
{
}
// Size ctor.
// Initialize an Array of size "pSize", with each element
// set to "value".
explicit Array (size_t pSize, const T& value = T ())
: m_size (pSize),
m_capacity (pSize),
m_array (new T[m_capacity])
{
fill (begin (), end (), value);
}
// Range ctor.
// Initialize an Array from the range [first, last).
// "first" and "last" must be Array iterators or pointers
// into a primitive array.
Array (const_iterator first, const_iterator last)
:m_size(distance(first,last)),m_capacity(m_size),m_array(new T[m_capacity])
{
copy(first,last,m_array);
}
// Reserve capacity for "space" elements.
// "space" must be greater than capacity.
// If not, leave the capacity unchanged.
// "size" must remain unchanged.
void
reserve (size_t space)
{
if (space > capacity ())
{
T* array = new T[space];
copy (begin (), end (), array);
delete[] m_array;
m_array = array;
m_capacity = space;
}
}
// Change the size to be "newSize".
// If "newSize" is less than "size",
// erase the last elements.
// If "newSize" is more than "size",
// insert "value"-s at the end.
void
resize (size_t newSize, const T& value = T ())
{
// TODO!
}
// Insert "item" before "pos", and return iterator pointing to "item".
// If the capacity is insufficient, DOUBLE it.
// If the capacity is 0, increase it to 1.
// NOTE: If a reallocation occurs, "pos" will be invalidated!
iterator
insert (iterator pos, const T& item)
{
// TODO!
}
// Remove element at "pos", and return an iterator
// referencing the next element.
iterator
erase (iterator pos)
{
// TODO!
}
// Return iterator pointing to the first element.
iterator
begin ()
{
// TODO!
}
const_iterator
begin () const
{
return m_array;
}
// Return iterator pointing one beyond the last element.
iterator
end ()
{
// TODO!
}
const_iterator
end () const
{
// TODO!
}
// Return a pointer to the underlying dynamic array
T*
data ()
{
return m_array;
}
// Return a pointer to the underlying dynamic array
T const*
data () const
{
return m_array;
}
private:
// Stores the number of elements in the Array.
size_t m_size;
// Stores the capacity of the Array, which must be at least "m_size".
size_t m_capacity;
// Stores a pointer to the first element in the Array.
T* m_array;
};
#endif
Some Hints:



iterator insert (iterator pos, const T\& value); Insert value before pos, and return an iterator pointing to the position of the new value in the vector. The operation may invalidate any existing iterators if the capacity needs increased. Postcondition: The vector has a new element. void resize (size_type n, const T& fill =T() ); Modify the size of the vector. If the size is increased, the value fill is added to the elements on the tail of the vector. If the size is decreased, the original values at the front are retained. Postcondition: The vector has size n. iterator erase (iterator pos); Erase the element pointed to by pos. Precondition: The vector is not empty. Postcondition: The vector has one fewer element
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
