Question: #include #include #include #include /************************************************************/ // Local includes /************************************************************/ // Using declarations // YOU DO NOT NECESSARILY NEED TO USE ALL OF THESE! using std::copy;

#include

#include

#include

#include

/************************************************************/

// Local includes

/************************************************************/

// Using declarations

// YOU DO NOT NECESSARILY NEED TO USE ALL OF THESE!

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

}

// TODO!

// 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);

}

// TODO!

// Copy ctor.

// Initialize this object from "a".

Array (const Array& a)

:m_size(a.m_size),m_capacity(a.m_capacity),m_array(new T[m_capacity])

{

copy(a.begin(),a.end(),m_array);

}

// TODO!

// Destructor.

// Release allocated memory.

~Array ()

{

delete[] m_array;

}

// TODO!

// Assignment operator.

// Assign "a" to this object.

// Be careful to check for self-assignment.

Array&

operator= (const Array& a)

{

if (this != &a)

{

delete[] m_array;

m_size = a.m_size;

m_capacity = a.m_capacity;

m_array = new T[m_capacity];

copy(a.begin(),a.end(),m_array);

}

return *this;

}

// Return the size.

size_t

size () const

{

return m_size;

}

// TODO!

// Return true if this Array is empty, false o/w.

bool

empty () const

{

return m_size == 0;

}

// TODO!

// Return the capacity.

size_t

capacity () const

{

return m_capacity;

}

// TODO!

// Return the element at position "index".

T& operator[] (size_t index)

{

return m_array[index];

}

// TODO!

const T& operator[] (size_t index) const

{

return m_array[index];

}

// TODO!

// Insert an element at the back.

// If the capacity is insufficient, DOUBLE it.

// If the capacity is 0, increase it to 1.

void

push_back (const T& item)

{

if (m_size == 0)

{

m_capacity = 1;

m_array = new T[m_capacity];

}

else if (m_size == m_capacity)

{

m_capacity *= 2;

T* temp = new T[2 * m_capacity];

copy(begin(),end(),temp);

delete[] m_array;

m_array = temp;

}

m_array[m_size] = item;

++m_size;

}

// TODO!

// Erase the element at the back.

void

pop_back ()

{

if (m_size > 0)

{

--m_size;

}

}

// 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;

}

}

// TODO!

// 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 ())

{

if (newSize == m_size)

{

return;

}

if (newSize < m_size)

{

m_size = newSize;

return;

}

if (newSize > m_capacity)

{

reserve(newSize);

}

fill (begin (), end (), value);

m_size = newSize;

}

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

{

copy(pos + 1, end(), pos);

-- m_size;

return pos;

}

// TODO!

// Return iterator pointing to the first element.

iterator

begin ()

{

return m_array;

}

// TODO!

const_iterator

begin () const

{

return m_array;

}

// TODO!

// Return iterator pointing one beyond the last element.

iterator

end ()

{

return m_array + m_size;

}

// TODO!

const_iterator

end () const

{

return m_array + m_size;

}

// 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;

};

Fix the below error with the member function resize:

TestArray.hpp:747: FAILED: REQUIRE( Counters::cctor + Counters::cassign >= arr.size () ) with expansion: 26 >= 100 with messages: original := [ 8 7 -3 -12 -1 15 20 -18 -7 -13 -4 -25 5 ] arr := [ 65 65 65 65 65 65 65 65 65 65 65 65 65 5 26 8 -12 7 15 21 -23 -18 - 11 11 -21 -12 -10 -12 -12 5 -18 -23 -10 -17 3 -2 -23 15 -4 -17 1 26 -4 26 4 - 25 -14 18 8 6 -8 12 15 -14 -25 20 -21 23 -18 -6 1 22 -3 -3 -21 26 21 -18 -7 22 23 21 21 -8 -1 -23 25 11 1 7 -10 19 24 10 10 25 -17 15 21 23 -18 -26 18 - 15 -3 23 16 -9 10 -17 ] number of elements copied should be equal to new size

Help with

iterator

insert (iterator pos, const T& item)

{

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!