-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.h
More file actions
97 lines (90 loc) · 3.49 KB
/
Vector.h
File metadata and controls
97 lines (90 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// ECE4893/8893 Assignment 3
// Georgia Tech, Fall 2011
// Students should NOT hae to change this, excepting
// commenting out the GRAD_STUDENT definition (for undergrads)
// This is the definition of the templated "Vector" class
// that stores a variable number of elements of arbitrary
// type T.
// First is the "include guard
#ifndef __VECTOR_H__
#define __VECTOR_H__
// unistd.h is needed for size_t
#include <unistd.h>
// Comment out below if not a grad student
//#define GRAD_STUDENT
// Forward declaration of VectorIterator (templated) class
template <typename T> class VectorIterator;
template <typename T>
class Vector
{
public:
// Define the iterator type based on VectorIterator below
// Construcors
Vector(); // Default constructor
Vector(const Vector&); // Copy constructor
Vector& operator=(const Vector& rhs); // Assignment operator
#ifdef GRAD_STUDENT
// Grad students only, implement next three
Vector(size_t); // Construct with initial "reserved" size
Vector(size_t n, const T& t); // Construct with "n" copies of t.
void Reserve(size_t n); // Ensure enough memory for "n" elements
#endif
// Destructor
~Vector(); // Destructor
// Add and remove elements
// Note. The normal Standard Template Library vector does not
// have push_front" or "pop_front". We will implement them anyway
void Push_Back(const T&); // Add to end of vector
void Push_Front(const T&); // Add to front of vector
void Pop_Back(); // Remove last element
void Pop_Front(); // Remove first element
// Element access
T& Front() const; // Return reference to first element
T& Back() const; // Return reference to last element
const T& operator[](size_t) const;// Return reference to specified element
T& operator[](size_t); // Return reference to specified (nonconst)
// Metrics functions
size_t Size() const; // Return number of elements
bool Empty() const; // True if empty
// Clear the vecor
void Clear(); // Remove all elements
// Define the iterator
// Iterator initializers
VectorIterator<T> Begin() const; // Get an iterator to first element
VectorIterator<T> End() const; // Get an iterator to one beyond end
#ifdef GRAD_STUDENT
// Grad students only, implement the next two
void Erase(const VectorIterator<T>&); // Remove specified element
void Insert(const T&, const VectorIterator<T>&); // Insert element BEFORE itr
#endif
private:
// Member variables here
T* elements; // Points to the actual elements
size_t count; // Number of elements in the vector
size_t reserved; // Reserved memory count
};
// Define the VectorIterator class
template <typename T>
class VectorIterator
{
public:
// Define your iterator constructor here
VectorIterator();
VectorIterator(T*);
VectorIterator(const VectorIterator&);
// De-referencing, and incrementing operators
// Note we are not implementing the "arrow" (->) operator
// Note we are not implementing the decrementing operators
T& operator*() const;
VectorIterator operator++(); // Prefix increment
VectorIterator operator++(int); // Postfix increment
// Comparison operators
bool operator !=(const VectorIterator&) const;
bool operator ==(const VectorIterator&) const;
private:
//Member variables
T* current;
// Allow the Vector class access to private members
friend class Vector<T>;
};
#endif