Updated Constructor and copy.

This commit is contained in:
Yohan Boujon 2023-09-28 14:58:41 +02:00
parent 8d9c4cfe82
commit 596ba25cc0
2 changed files with 60 additions and 28 deletions

View file

@ -4,37 +4,40 @@
#include <cstddef> #include <cstddef>
#include <ostream> #include <ostream>
template<class T> template <class T>
class Vector{ class Vector {
public: public:
Vector(); Vector();
Vector(size_t capacity); Vector(size_t capacity);
Vector(size_t capacity, T value); Vector(size_t capacity, T value);
Vector(const Vector<T>& other);
~Vector(); ~Vector();
size_t Size(); size_t Size();
size_t Capacity(); size_t Capacity();
void PushBack(T value); void PushBack(T value);
template<class Tfriend> template <class Tfriend>
friend std::ostream& operator<<(std::ostream& stream, const Vector<Tfriend>& vec); friend std::ostream& operator<<(std::ostream& stream, const Vector<Tfriend>& vec);
template<class Tfriend> template <class Tfriend>
friend std::istream& operator>>(std::istream& stream, Vector<Tfriend>& vec); friend std::istream& operator>>(std::istream& stream, Vector<Tfriend>& vec);
Vector<T>& operator=(const Vector<T>& other);
private: private:
T* _values;
size_t _capacity; size_t _capacity;
size_t _size; size_t _size;
T* _values;
}; };
template<class T> template <class T>
Vector<T>::Vector() Vector<T>::Vector()
: _capacity(1) : _capacity(1)
, _size(0) , _size(0)
, _values(new T[1])
{ {
// A vector must at least have a capacity of 1
_values = new T[1];
} }
template<class T> template <class T>
Vector<T>::Vector(size_t capacity) Vector<T>::Vector(size_t capacity)
: _capacity(capacity) : _capacity(capacity)
, _size(0) , _size(0)
{ {
@ -42,42 +45,64 @@ Vector<T>::Vector(size_t capacity)
_values = new T[capacity]; _values = new T[capacity];
} }
template<class T> template <class T>
Vector<T>::Vector(size_t capacity, T value) Vector<T>::Vector(size_t capacity, T value)
: _capacity(capacity) : _capacity(capacity)
, _size(capacity) , _size(capacity)
{ {
// Allocating the heap and setting the value to each element. // Allocating the heap and setting the value to each element.
_values = new T[capacity]; _values = new T[capacity];
for(size_t i = 0; i<capacity; i++) for (size_t i = 0; i < capacity; i++)
_values[i] = 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> template <class T>
Vector<T>::~Vector() Vector<T>::~Vector()
{ {
delete[] _values; delete[] _values;
} }
template<class T> 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; } size_t Vector<T>::Capacity() { return _capacity; }
template<class T> template <class T>
size_t Vector<T>::Size() { return _size; } size_t Vector<T>::Size() { return _size; }
template<class T> template <class T>
void Vector<T>::PushBack(T value) void Vector<T>::PushBack(T value)
{ {
// if the next size will overload the heap // if the next size will overload the heap
if (_capacity < _size+1) if (_capacity < _size + 1) {
{
// Keep a track of the old pointer // Keep a track of the old pointer
T* oldPtr = _values; T* oldPtr = _values;
// Creating a new pointer by resizing its capacity by factor 2. // Creating a new pointer by resizing its capacity by factor 2.
_values = new T[_size*2]; _values = new T[_size * 2];
_capacity*=2; _capacity *= 2;
// Copying the old array onto the new // Copying the old array onto the new
for(size_t i=0; i<_size; i++) for (size_t i = 0; i < _size; i++)
_values[i] = oldPtr[i]; _values[i] = oldPtr[i];
// Deallocating the old array // Deallocating the old array
delete[] oldPtr; delete[] oldPtr;
@ -88,22 +113,22 @@ void Vector<T>::PushBack(T value)
} }
// methode get // methode get
template<class T> template <class T>
std::ostream& operator<<(std::ostream& stream, const Vector<T>& vec) std::ostream& operator<<(std::ostream& stream, const Vector<T>& vec)
{ {
for(size_t i=0; i<vec._size; i++) for (size_t i = 0; i < vec._size; i++)
stream << vec._values[i] << ", "; stream << vec._values[i] << ", ";
return stream; return stream;
} }
// methode put // methode put
template<class T> template <class T>
std::istream& operator>>(std::istream& stream, Vector<T>& vec) std::istream& operator>>(std::istream& stream, Vector<T>& vec)
{ {
for(size_t i=0; i<vec._capacity; i++) for (size_t i = 0; i < vec._capacity; i++)
stream >> vec._values[i]; stream >> vec._values[i];
vec._size = vec._capacity; vec._size = vec._capacity;
return stream; return stream;
} }
#endif //HEADER_VECTOR #endif // HEADER_VECTOR

View file

@ -15,6 +15,13 @@ int main(void)
std::cout << "Capacity of charVector:" << charVector.Capacity() << "\t Size of charVector: " << charVector.Size() << std::endl; std::cout << "Capacity of charVector:" << charVector.Capacity() << "\t Size of charVector: " << charVector.Size() << std::endl;
std::cin >> charVector; std::cin >> charVector;
std::cout << "char vector: " << charVector << std::endl; 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; return 0;
} }