Updated Constructor and copy.
This commit is contained in:
parent
8d9c4cfe82
commit
596ba25cc0
2 changed files with 60 additions and 28 deletions
|
@ -10,6 +10,7 @@ public:
|
|||
Vector();
|
||||
Vector(size_t capacity);
|
||||
Vector(size_t capacity, T value);
|
||||
Vector(const Vector<T>& other);
|
||||
~Vector();
|
||||
size_t Size();
|
||||
size_t Capacity();
|
||||
|
@ -18,19 +19,21 @@ public:
|
|||
friend std::ostream& operator<<(std::ostream& stream, const Vector<Tfriend>& vec);
|
||||
template <class Tfriend>
|
||||
friend std::istream& operator>>(std::istream& stream, Vector<Tfriend>& vec);
|
||||
|
||||
Vector<T>& operator=(const Vector<T>& other);
|
||||
|
||||
private:
|
||||
T* _values;
|
||||
size_t _capacity;
|
||||
size_t _size;
|
||||
T* _values;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
Vector<T>::Vector()
|
||||
: _capacity(1)
|
||||
, _size(0)
|
||||
, _values(new T[1])
|
||||
{
|
||||
// A vector must at least have a capacity of 1
|
||||
_values = new T[1];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
|
@ -53,6 +56,15 @@ Vector<T>::Vector(size_t capacity, T value)
|
|||
_values[i] = value;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Vector<T>::Vector(const Vector& other)
|
||||
: _capacity(other._capacity)
|
||||
, _size(other._size)
|
||||
, _values(new T[other._capacity])
|
||||
{
|
||||
for (size_t i = 0; i < other._size; i++)
|
||||
_values[i] = other._values[i];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Vector<T>::~Vector()
|
||||
|
@ -60,6 +72,20 @@ Vector<T>::~Vector()
|
|||
delete[] _values;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Vector<T>& Vector<T>::operator=(const Vector<T>& other)
|
||||
{
|
||||
if (this != &other) {
|
||||
this->_capacity = other._capacity;
|
||||
this->_size = other._size;
|
||||
delete[] this->_values;
|
||||
this->_values = new T[other._capacity];
|
||||
for (size_t i = 0; i < other._size; i++)
|
||||
this->_values[i] = other._values[i];
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
size_t Vector<T>::Capacity() { return _capacity; }
|
||||
template <class T>
|
||||
|
@ -69,8 +95,7 @@ template<class T>
|
|||
void Vector<T>::PushBack(T value)
|
||||
{
|
||||
// if the next size will overload the heap
|
||||
if (_capacity < _size+1)
|
||||
{
|
||||
if (_capacity < _size + 1) {
|
||||
// Keep a track of the old pointer
|
||||
T* oldPtr = _values;
|
||||
// Creating a new pointer by resizing its capacity by factor 2.
|
||||
|
|
|
@ -15,6 +15,13 @@ int main(void)
|
|||
std::cout << "Capacity of charVector:" << charVector.Capacity() << "\t Size of charVector: " << charVector.Size() << std::endl;
|
||||
std::cin >> charVector;
|
||||
std::cout << "char vector: " << charVector << std::endl;
|
||||
|
||||
Vector<char> anotherVector = charVector;
|
||||
std::cout << "char vector: " << charVector << std::endl;
|
||||
|
||||
Vector<char> copiedVector;
|
||||
copiedVector = anotherVector;
|
||||
std::cout << "char vector: " << charVector << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue