Question: template class Array { public: // Some standard Container type aliases using value_type = T; // Iterators are just pointers to objects of type T

template

class Array

{

public:

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

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

{

}

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;

};

Help with the member functions: resize, and insert in C++.

Some hints:

1. 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.

2.

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.

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!