SlideShare a Scribd company logo
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);
}
// 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);
}
// 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);
}
// Destructor.
// Release allocated memory.
~Array ()
{
delete[] m_array;
}
// Assignment operator.
// Assign "a" to this object.
// Be careful to check for self-assignment.
Array&
operator= (const Array& a)
{
if (this != &a)
{
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;
}
// Return true if this Array is empty, false o/w.
bool
empty () const
{
return m_size == 0;
}
// Return the capacity.
size_t
capacity () const
{
return m_capacity;
}
// Return the element at position "index".
T& operator[] (size_t index)
{
return m_array[index];
}
const T& operator[] (size_t index) const
{
return m_array[index];
}
// 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;
}
// 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;
}
}
// 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;
}
// 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)
{
}
// 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;
}
// Return iterator pointing to the first element.
iterator
begin ()
{
return m_array;
}
const_iterator
begin () const
{
return m_array;
}
// Return iterator pointing one beyond the last element.
iterator
end ()
{
return m_array + m_size;
}
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;
};
Test this custom vector class "Array" to see if the code works and help with
iterator
insert (iterator pos, const T& item)
{
}
please!

More Related Content

PDF
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
PDF
template-typename T- class Array { public- using value_type - T- -- It (1).pdf
DOCX
I am trying to fill out a program where the method definitions will b.docx
PPT
Vector3
PPT
Object oriented programming; operator overloading array
PPTX
Object Oriented Design and Programming Unit-05
PDF
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
PDF
Given below is the code for the question. Since the test files (ment.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
template-typename T- class Array { public- using value_type - T- -- It (1).pdf
I am trying to fill out a program where the method definitions will b.docx
Vector3
Object oriented programming; operator overloading array
Object Oriented Design and Programming Unit-05
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
Given below is the code for the question. Since the test files (ment.pdf

Similar to template-typename T- class Array { public- ---------------------------.pdf (20)

PPTX
Алексей Кутумов, Вектор с нуля
DOC
Oops lab manual2
PPTX
C++11 - STL Additions
PDF
I really need help with my C++ assignment. The following is the info.pdf
PPT
Operator overloading Object Oriented Programming
PDF
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
PDF
Please implement Stack using Array (capacity 100)- Use template to.pdf
ODT
(3) cpp abstractions more_on_user_defined_types_exercises
PDF
C++ normal assignments by maharshi_jd.pdf
PDF
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
PPT
Operator overloading
PPTX
week14Pointers_II. pointers pemrograman dasar C++.pptx
PPTX
Vector class in C++
PPTX
Data structure and Algorithms (C++).pptx
PDF
Sec 06 Basics Data Structure & Array Implementation .pdf
PPTX
14. containers, vector, list
PPTX
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
PPTX
Arrays matrix 2020 ab
PDF
Write a class ArrayList that represents an array of integers. Init.pdf
PDF
Implement the sequence class from Section 3.2 of the textbook. The d.pdf
Алексей Кутумов, Вектор с нуля
Oops lab manual2
C++11 - STL Additions
I really need help with my C++ assignment. The following is the info.pdf
Operator overloading Object Oriented Programming
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Please implement Stack using Array (capacity 100)- Use template to.pdf
(3) cpp abstractions more_on_user_defined_types_exercises
C++ normal assignments by maharshi_jd.pdf
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
Operator overloading
week14Pointers_II. pointers pemrograman dasar C++.pptx
Vector class in C++
Data structure and Algorithms (C++).pptx
Sec 06 Basics Data Structure & Array Implementation .pdf
14. containers, vector, list
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
Arrays matrix 2020 ab
Write a class ArrayList that represents an array of integers. Init.pdf
Implement the sequence class from Section 3.2 of the textbook. The d.pdf
Ad

More from ashokadyes (20)

PDF
Terando Center began operations on July 1 - It uses a perpetual invent (1).pdf
PDF
Television and radio stations use four call letters starting with W or.pdf
PDF
Tesla made an investment in China- Below are the cash flows (in billio.pdf
PDF
Term for a microbe that can grow in the ocean- an estuary- and a river.pdf
PDF
TB MC Qu- 90 Assume you can exchange-- Mised chorer perse tin64 5t321.pdf
PDF
TB MC Qu- 01-34 What is substantive law- What is substantive law- Mult.pdf
PDF
Television- newspapers- the Internet- and magazines are just a few of.pdf
PDF
The Achilles heel (or biggest disadvantage-danger-pitfall) of relying.pdf
PDF
Task environments can take on several different characteristics- Match.pdf
PDF
Telsa issues Fixed- Intel Issues floating- bp is basis point (100 bp i.pdf
PDF
TB MC Qu- 13-76 Genesis Scents has two divisions- the Cologne--- price.pdf
PDF
TechPro Inc-- a leading laptop manufacturing company- manufactures all.pdf
PDF
The acoompanying table describes resulls from groups of 8 births from.pdf
PDF
The accompanying unions and labor law data reports the percent of publ.pdf
PDF
The ability for a company to meet its liability obligations is importa.pdf
PDF
The accompanying table describes the random variable x- the numbers of (1).pdf
PDF
Task- Review the medical documentation for the patient progress notes.pdf
PDF
The ABO human blood types are due to the presence of carbohydrates on.pdf
PDF
The Abstraction of address spaces- Now run pmap on some of these proce.pdf
PDF
The above case study highlighted the rise of trade unions in South Afr.pdf
Terando Center began operations on July 1 - It uses a perpetual invent (1).pdf
Television and radio stations use four call letters starting with W or.pdf
Tesla made an investment in China- Below are the cash flows (in billio.pdf
Term for a microbe that can grow in the ocean- an estuary- and a river.pdf
TB MC Qu- 90 Assume you can exchange-- Mised chorer perse tin64 5t321.pdf
TB MC Qu- 01-34 What is substantive law- What is substantive law- Mult.pdf
Television- newspapers- the Internet- and magazines are just a few of.pdf
The Achilles heel (or biggest disadvantage-danger-pitfall) of relying.pdf
Task environments can take on several different characteristics- Match.pdf
Telsa issues Fixed- Intel Issues floating- bp is basis point (100 bp i.pdf
TB MC Qu- 13-76 Genesis Scents has two divisions- the Cologne--- price.pdf
TechPro Inc-- a leading laptop manufacturing company- manufactures all.pdf
The acoompanying table describes resulls from groups of 8 births from.pdf
The accompanying unions and labor law data reports the percent of publ.pdf
The ability for a company to meet its liability obligations is importa.pdf
The accompanying table describes the random variable x- the numbers of (1).pdf
Task- Review the medical documentation for the patient progress notes.pdf
The ABO human blood types are due to the presence of carbohydrates on.pdf
The Abstraction of address spaces- Now run pmap on some of these proce.pdf
The above case study highlighted the rise of trade unions in South Afr.pdf
Ad

Recently uploaded (20)

PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Classroom Observation Tools for Teachers
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
01-Introduction-to-Information-Management.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Trump Administration's workforce development strategy
PPTX
Cell Structure & Organelles in detailed.
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Computing-Curriculum for Schools in Ghana
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Classroom Observation Tools for Teachers
Microbial disease of the cardiovascular and lymphatic systems
Yogi Goddess Pres Conference Studio Updates
01-Introduction-to-Information-Management.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Trump Administration's workforce development strategy
Cell Structure & Organelles in detailed.
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
STATICS OF THE RIGID BODIES Hibbelers.pdf
Microbial diseases, their pathogenesis and prophylaxis
Final Presentation General Medicine 03-08-2024.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Supply Chain Operations Speaking Notes -ICLT Program
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Computing-Curriculum for Schools in Ghana
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE

template-typename T- class Array { public- ---------------------------.pdf

  • 1. 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),
  • 2. 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); }
  • 3. // 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); } // Destructor. // Release allocated memory. ~Array () { delete[] m_array; } // Assignment operator. // Assign "a" to this object. // Be careful to check for self-assignment. Array& operator= (const Array& a) { if (this != &a) { m_size = a.m_size; m_capacity = a.m_capacity;
  • 4. 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; } // Return true if this Array is empty, false o/w. bool empty () const { return m_size == 0; } // Return the capacity. size_t capacity () const { return m_capacity; }
  • 5. // Return the element at position "index". T& operator[] (size_t index) { return m_array[index]; } const T& operator[] (size_t index) const { return m_array[index]; } // 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;
  • 6. T* temp = new T[2 * m_capacity]; copy(begin(),end(),temp); delete[] m_array; m_array = temp; } m_array[m_size] = item; ++m_size; } // 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)
  • 7. { 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 ()) { if (newSize == m_size) { return; } if (newSize < m_size)
  • 8. { m_size = newSize; return; } if (newSize > m_capacity) { reserve(newSize); } fill (begin (), end (), value); m_size = newSize; } // 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) { } // Remove element at "pos", and return an iterator // referencing the next element. iterator erase (iterator pos)
  • 9. { copy(pos + 1, end(), pos); -- m_size; return pos; } // Return iterator pointing to the first element. iterator begin () { return m_array; } const_iterator begin () const { return m_array; } // Return iterator pointing one beyond the last element. iterator end () { return m_array + m_size; } const_iterator
  • 10. 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;
  • 11. }; Test this custom vector class "Array" to see if the code works and help with iterator insert (iterator pos, const T& item) { } please!