-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.h
More file actions
109 lines (80 loc) · 3.3 KB
/
Vector.h
File metadata and controls
109 lines (80 loc) · 3.3 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
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef COP4530_VECTOR_H
#define COP4530_VECTOR_H
/********************************
* COP 4530
* FSU Department of Computer Sience
********************************/
#include <iostream>
#include <stdexcept>
namespace cop4530 {
template <typename T>
class Vector {
public:
// iterator support
/* typedef T* iterator; */
/* typedef const T* const_iterator; */
// this is the new syntax in C++11
using iterator = T*;
using const_iterator = const T*;
// constructor, destructor, and copy constructor
Vector(); // default zero parameter constructor
Vector(const Vector &rhs); // copy constructor
Vector(Vector &&rhs); // move constructor
// num elements with value of val
explicit Vector(int num, const T& val = T());
// constructs with elements [start, end)
Vector(const_iterator start, const_iterator end);
~Vector(); // destructor
// operators
// index operator
T& operator[](int index);
const T& operator[](int index) const;
// copy assignment operator
const Vector& operator=(const Vector &rhs);
// move assignment operator
Vector & operator=(Vector &&rhs);
// at function. safer than operator[]
T& at(int loc );
const T& at(int loc ) const;
T &front(); // reference to the first element
const T& front() const;
T &back(); // reference to the last element
const T & back() const;
// accessor member functions
int size() const; // number of elements
int capacity() const; // capacity of vector
bool empty() const; // check if list is empty
// mutator member functions
void clear(); // delete all elements
void push_back(const T & val); // insert to the end
void pop_back(); // delete last element
void resize(int num, T val = T()); // resize vector
void reserve(int newCapacity); // reserve memory
// print out all elements. ofc is deliminitor
void print(std::ostream& os, char ofc = ' ') const;
// iterator support
iterator begin(); // iterator to first element
const_iterator begin() const;
iterator end(); // end marker iterator
const_iterator end() const;
iterator insert(iterator itr, const T& val); // insert val ahead of itr
iterator erase(iterator itr); // erase one element
iterator erase(iterator start, iterator end); // erase [start, end)
private:
int theSize; // number of elements
int theCapacity; // number of elements that can be stored
T *array; // dynamic array to store elements.
void doubleCapacity();
}; // end of class Vector
// overloading comparison operators
template <typename T>
bool operator==(const Vector<T> & lhs, const Vector<T> &rhs);
template <typename T>
bool operator!=(const Vector<T> & lhs, const Vector<T> &rhs);
// overloading output operator
template <typename T>
std::ostream & operator<<(std::ostream &os, const Vector<T> &v);
// include the implementation file here
#include "Vector.hpp"
} // end of namespace COP4530
#endif