SlideShare a Scribd company logo
1 of 13
Download to read offline
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <iterator>
/************************************************************/
// 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<typename T>
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:
Help with
iterator
insert (iterator pos, const T& item)
{
}
C++ Array Template Class Implementation

More Related Content

Similar to C++ Array Template Class Implementation

@author Derek Harter @cwid 123 45 678 @class .docx
@author Derek Harter  @cwid   123 45 678  @class  .docx@author Derek Harter  @cwid   123 45 678  @class  .docx
@author Derek Harter @cwid 123 45 678 @class .docxadkinspaige22
 
template-typename T- class Array { public- using value_type - T- -- It (1).pdf
template-typename T- class Array { public- using value_type - T- -- It (1).pdftemplate-typename T- class Array { public- using value_type - T- -- It (1).pdf
template-typename T- class Array { public- using value_type - T- -- It (1).pdfashokadyes
 
Were writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdfWere writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdffsenterprises
 
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdfNeed Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdfEdwardw5nSlaterl
 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdfafgt2012
 
Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more steviesellars
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfaksahnan
 
Fix my codeCode.pdf
Fix my codeCode.pdfFix my codeCode.pdf
Fix my codeCode.pdfConint29
 
include ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdfinclude ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdfnaslin841216
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docxVictorXUQGloverl
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfsales98
 
include ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdfinclude ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdfaathmiboutique
 
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docxAvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docxrock73
 
Please complete ALL of the �TO DO�s in this code. I am really strugg.pdf
Please complete ALL of the �TO DO�s in this code. I am really strugg.pdfPlease complete ALL of the �TO DO�s in this code. I am really strugg.pdf
Please complete ALL of the �TO DO�s in this code. I am really strugg.pdfsupport58
 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfARCHANASTOREKOTA
 
Character.cpphpp givenCharacter.cpp#include Character.hp.pdf
Character.cpphpp givenCharacter.cpp#include Character.hp.pdfCharacter.cpphpp givenCharacter.cpp#include Character.hp.pdf
Character.cpphpp givenCharacter.cpp#include Character.hp.pdftxkev
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxssuser454af01
 
In this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfIn this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfcontact41
 

Similar to C++ Array Template Class Implementation (20)

@author Derek Harter @cwid 123 45 678 @class .docx
@author Derek Harter  @cwid   123 45 678  @class  .docx@author Derek Harter  @cwid   123 45 678  @class  .docx
@author Derek Harter @cwid 123 45 678 @class .docx
 
template-typename T- class Array { public- using value_type - T- -- It (1).pdf
template-typename T- class Array { public- using value_type - T- -- It (1).pdftemplate-typename T- class Array { public- using value_type - T- -- It (1).pdf
template-typename T- class Array { public- using value_type - T- -- It (1).pdf
 
Were writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdfWere writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdf
 
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdfNeed Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf
 
Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
 
Fix my codeCode.pdf
Fix my codeCode.pdfFix my codeCode.pdf
Fix my codeCode.pdf
 
include ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdfinclude ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdf
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
 
Js hacks
Js hacksJs hacks
Js hacks
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdf
 
include ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdfinclude ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdf
 
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docxAvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
 
Please complete ALL of the �TO DO�s in this code. I am really strugg.pdf
Please complete ALL of the �TO DO�s in this code. I am really strugg.pdfPlease complete ALL of the �TO DO�s in this code. I am really strugg.pdf
Please complete ALL of the �TO DO�s in this code. I am really strugg.pdf
 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdf
 
Character.cpphpp givenCharacter.cpp#include Character.hp.pdf
Character.cpphpp givenCharacter.cpp#include Character.hp.pdfCharacter.cpphpp givenCharacter.cpp#include Character.hp.pdf
Character.cpphpp givenCharacter.cpp#include Character.hp.pdf
 
Link list
Link listLink list
Link list
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
 
In this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfIn this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdf
 

More from BANSALANKIT1077

(1 point) The age distribution for senators in the 104th U-S- Congress.pdf
(1 point) The age distribution for senators in the 104th U-S- Congress.pdf(1 point) The age distribution for senators in the 104th U-S- Congress.pdf
(1 point) The age distribution for senators in the 104th U-S- Congress.pdfBANSALANKIT1077
 
(1 point) The combined math and verbal scores for females taking the S.pdf
(1 point) The combined math and verbal scores for females taking the S.pdf(1 point) The combined math and verbal scores for females taking the S.pdf
(1 point) The combined math and verbal scores for females taking the S.pdfBANSALANKIT1077
 
(1 point) In a survey of 269 people- the following data were obtained.pdf
(1 point) In a survey of 269 people- the following data were obtained.pdf(1 point) In a survey of 269 people- the following data were obtained.pdf
(1 point) In a survey of 269 people- the following data were obtained.pdfBANSALANKIT1077
 
(0) Assignment 4-11- Adding Cursor Flexibility An administration page.pdf
(0) Assignment 4-11- Adding Cursor Flexibility An administration page.pdf(0) Assignment 4-11- Adding Cursor Flexibility An administration page.pdf
(0) Assignment 4-11- Adding Cursor Flexibility An administration page.pdfBANSALANKIT1077
 
( 2 points) In a survey of 270 people- the following data were obtaine.pdf
( 2 points) In a survey of 270 people- the following data were obtaine.pdf( 2 points) In a survey of 270 people- the following data were obtaine.pdf
( 2 points) In a survey of 270 people- the following data were obtaine.pdfBANSALANKIT1077
 
(----IN C) Incorrect output on some test cases- I'm using a linked lis.pdf
(----IN C) Incorrect output on some test cases- I'm using a linked lis.pdf(----IN C) Incorrect output on some test cases- I'm using a linked lis.pdf
(----IN C) Incorrect output on some test cases- I'm using a linked lis.pdfBANSALANKIT1077
 
'ulcan Flyovers offers scenic overflights of Mount Saint Helens- the v.pdf
'ulcan Flyovers offers scenic overflights of Mount Saint Helens- the v.pdf'ulcan Flyovers offers scenic overflights of Mount Saint Helens- the v.pdf
'ulcan Flyovers offers scenic overflights of Mount Saint Helens- the v.pdfBANSALANKIT1077
 
'ear- What is the cost of common equity- Round your answer to two deci.pdf
'ear- What is the cost of common equity- Round your answer to two deci.pdf'ear- What is the cost of common equity- Round your answer to two deci.pdf
'ear- What is the cost of common equity- Round your answer to two deci.pdfBANSALANKIT1077
 
( information source - automotive trade body) Note- The information a.pdf
(  information source - automotive trade body) Note- The information a.pdf(  information source - automotive trade body) Note- The information a.pdf
( information source - automotive trade body) Note- The information a.pdfBANSALANKIT1077
 
'Sense giving' can also be referred to as- developing a recipe that th.pdf
'Sense giving' can also be referred to as- developing a recipe that th.pdf'Sense giving' can also be referred to as- developing a recipe that th.pdf
'Sense giving' can also be referred to as- developing a recipe that th.pdfBANSALANKIT1077
 
#2- Mary Decker- a 4-year-old who is covered under the CHIP program- p.pdf
#2- Mary Decker- a 4-year-old who is covered under the CHIP program- p.pdf#2- Mary Decker- a 4-year-old who is covered under the CHIP program- p.pdf
#2- Mary Decker- a 4-year-old who is covered under the CHIP program- p.pdfBANSALANKIT1077
 
#4 concepts of programming language question- (a) Explain the advantag.pdf
#4 concepts of programming language question- (a) Explain the advantag.pdf#4 concepts of programming language question- (a) Explain the advantag.pdf
#4 concepts of programming language question- (a) Explain the advantag.pdfBANSALANKIT1077
 
(2) Twelve major earthquakes on Richter mapnitades had shown here- Fin.pdf
(2) Twelve major earthquakes on Richter mapnitades had shown here- Fin.pdf(2) Twelve major earthquakes on Richter mapnitades had shown here- Fin.pdf
(2) Twelve major earthquakes on Richter mapnitades had shown here- Fin.pdfBANSALANKIT1077
 
## Part B- Conway's Game of Life ### Introduction John Conway's Ga.pdf
## Part B- Conway's Game of Life   ### Introduction   John Conway's Ga.pdf## Part B- Conway's Game of Life   ### Introduction   John Conway's Ga.pdf
## Part B- Conway's Game of Life ### Introduction John Conway's Ga.pdfBANSALANKIT1077
 
(2 pt) DNA transposons can be as short as 80bp- although the transposa.pdf
(2 pt) DNA transposons can be as short as 80bp- although the transposa.pdf(2 pt) DNA transposons can be as short as 80bp- although the transposa.pdf
(2 pt) DNA transposons can be as short as 80bp- although the transposa.pdfBANSALANKIT1077
 
(10 pts)- Let the random variable X have the following CDF- Find P(0X-.pdf
(10 pts)- Let the random variable X have the following CDF- Find P(0X-.pdf(10 pts)- Let the random variable X have the following CDF- Find P(0X-.pdf
(10 pts)- Let the random variable X have the following CDF- Find P(0X-.pdfBANSALANKIT1077
 
(10 peints) Given the Pceitional Liat clak belaw (implemented with a d.pdf
(10 peints) Given the Pceitional Liat clak belaw (implemented with a d.pdf(10 peints) Given the Pceitional Liat clak belaw (implemented with a d.pdf
(10 peints) Given the Pceitional Liat clak belaw (implemented with a d.pdfBANSALANKIT1077
 
(1) Briefly describe the terms sanitization- disinfection- sterilizati.pdf
(1) Briefly describe the terms sanitization- disinfection- sterilizati.pdf(1) Briefly describe the terms sanitization- disinfection- sterilizati.pdf
(1) Briefly describe the terms sanitization- disinfection- sterilizati.pdfBANSALANKIT1077
 
(1) Write out the POWER SET- Keep in mind a subset can be made of any.pdf
(1) Write out the POWER SET- Keep in mind a subset can be made of any.pdf(1) Write out the POWER SET- Keep in mind a subset can be made of any.pdf
(1) Write out the POWER SET- Keep in mind a subset can be made of any.pdfBANSALANKIT1077
 
(1) Question- Describe the five components of an Information System- (.pdf
(1) Question- Describe the five components of an Information System- (.pdf(1) Question- Describe the five components of an Information System- (.pdf
(1) Question- Describe the five components of an Information System- (.pdfBANSALANKIT1077
 

More from BANSALANKIT1077 (20)

(1 point) The age distribution for senators in the 104th U-S- Congress.pdf
(1 point) The age distribution for senators in the 104th U-S- Congress.pdf(1 point) The age distribution for senators in the 104th U-S- Congress.pdf
(1 point) The age distribution for senators in the 104th U-S- Congress.pdf
 
(1 point) The combined math and verbal scores for females taking the S.pdf
(1 point) The combined math and verbal scores for females taking the S.pdf(1 point) The combined math and verbal scores for females taking the S.pdf
(1 point) The combined math and verbal scores for females taking the S.pdf
 
(1 point) In a survey of 269 people- the following data were obtained.pdf
(1 point) In a survey of 269 people- the following data were obtained.pdf(1 point) In a survey of 269 people- the following data were obtained.pdf
(1 point) In a survey of 269 people- the following data were obtained.pdf
 
(0) Assignment 4-11- Adding Cursor Flexibility An administration page.pdf
(0) Assignment 4-11- Adding Cursor Flexibility An administration page.pdf(0) Assignment 4-11- Adding Cursor Flexibility An administration page.pdf
(0) Assignment 4-11- Adding Cursor Flexibility An administration page.pdf
 
( 2 points) In a survey of 270 people- the following data were obtaine.pdf
( 2 points) In a survey of 270 people- the following data were obtaine.pdf( 2 points) In a survey of 270 people- the following data were obtaine.pdf
( 2 points) In a survey of 270 people- the following data were obtaine.pdf
 
(----IN C) Incorrect output on some test cases- I'm using a linked lis.pdf
(----IN C) Incorrect output on some test cases- I'm using a linked lis.pdf(----IN C) Incorrect output on some test cases- I'm using a linked lis.pdf
(----IN C) Incorrect output on some test cases- I'm using a linked lis.pdf
 
'ulcan Flyovers offers scenic overflights of Mount Saint Helens- the v.pdf
'ulcan Flyovers offers scenic overflights of Mount Saint Helens- the v.pdf'ulcan Flyovers offers scenic overflights of Mount Saint Helens- the v.pdf
'ulcan Flyovers offers scenic overflights of Mount Saint Helens- the v.pdf
 
'ear- What is the cost of common equity- Round your answer to two deci.pdf
'ear- What is the cost of common equity- Round your answer to two deci.pdf'ear- What is the cost of common equity- Round your answer to two deci.pdf
'ear- What is the cost of common equity- Round your answer to two deci.pdf
 
( information source - automotive trade body) Note- The information a.pdf
(  information source - automotive trade body) Note- The information a.pdf(  information source - automotive trade body) Note- The information a.pdf
( information source - automotive trade body) Note- The information a.pdf
 
'Sense giving' can also be referred to as- developing a recipe that th.pdf
'Sense giving' can also be referred to as- developing a recipe that th.pdf'Sense giving' can also be referred to as- developing a recipe that th.pdf
'Sense giving' can also be referred to as- developing a recipe that th.pdf
 
#2- Mary Decker- a 4-year-old who is covered under the CHIP program- p.pdf
#2- Mary Decker- a 4-year-old who is covered under the CHIP program- p.pdf#2- Mary Decker- a 4-year-old who is covered under the CHIP program- p.pdf
#2- Mary Decker- a 4-year-old who is covered under the CHIP program- p.pdf
 
#4 concepts of programming language question- (a) Explain the advantag.pdf
#4 concepts of programming language question- (a) Explain the advantag.pdf#4 concepts of programming language question- (a) Explain the advantag.pdf
#4 concepts of programming language question- (a) Explain the advantag.pdf
 
(2) Twelve major earthquakes on Richter mapnitades had shown here- Fin.pdf
(2) Twelve major earthquakes on Richter mapnitades had shown here- Fin.pdf(2) Twelve major earthquakes on Richter mapnitades had shown here- Fin.pdf
(2) Twelve major earthquakes on Richter mapnitades had shown here- Fin.pdf
 
## Part B- Conway's Game of Life ### Introduction John Conway's Ga.pdf
## Part B- Conway's Game of Life   ### Introduction   John Conway's Ga.pdf## Part B- Conway's Game of Life   ### Introduction   John Conway's Ga.pdf
## Part B- Conway's Game of Life ### Introduction John Conway's Ga.pdf
 
(2 pt) DNA transposons can be as short as 80bp- although the transposa.pdf
(2 pt) DNA transposons can be as short as 80bp- although the transposa.pdf(2 pt) DNA transposons can be as short as 80bp- although the transposa.pdf
(2 pt) DNA transposons can be as short as 80bp- although the transposa.pdf
 
(10 pts)- Let the random variable X have the following CDF- Find P(0X-.pdf
(10 pts)- Let the random variable X have the following CDF- Find P(0X-.pdf(10 pts)- Let the random variable X have the following CDF- Find P(0X-.pdf
(10 pts)- Let the random variable X have the following CDF- Find P(0X-.pdf
 
(10 peints) Given the Pceitional Liat clak belaw (implemented with a d.pdf
(10 peints) Given the Pceitional Liat clak belaw (implemented with a d.pdf(10 peints) Given the Pceitional Liat clak belaw (implemented with a d.pdf
(10 peints) Given the Pceitional Liat clak belaw (implemented with a d.pdf
 
(1) Briefly describe the terms sanitization- disinfection- sterilizati.pdf
(1) Briefly describe the terms sanitization- disinfection- sterilizati.pdf(1) Briefly describe the terms sanitization- disinfection- sterilizati.pdf
(1) Briefly describe the terms sanitization- disinfection- sterilizati.pdf
 
(1) Write out the POWER SET- Keep in mind a subset can be made of any.pdf
(1) Write out the POWER SET- Keep in mind a subset can be made of any.pdf(1) Write out the POWER SET- Keep in mind a subset can be made of any.pdf
(1) Write out the POWER SET- Keep in mind a subset can be made of any.pdf
 
(1) Question- Describe the five components of an Information System- (.pdf
(1) Question- Describe the five components of an Information System- (.pdf(1) Question- Describe the five components of an Information System- (.pdf
(1) Question- Describe the five components of an Information System- (.pdf
 

Recently uploaded

BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 

Recently uploaded (20)

BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

C++ Array Template Class Implementation

  • 1. #include <algorithm> #include <cstdlib> #include <iostream> #include <iterator> /************************************************************/ // 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<typename T> class Array { public: //***************************************************** // DO NOT MODIFY THIS SECTION! // Some standard Container type aliases
  • 2. 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".
  • 3. 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]) {
  • 4. 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);
  • 5. } 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; }
  • 6. // 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]; }
  • 7. 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.
  • 8. // 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 ()) {
  • 9. 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)
  • 10. { } // 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 {
  • 11. 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*
  • 12. 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: Help with iterator insert (iterator pos, const T& item) { }