TD2 Finished.

This commit is contained in:
Yohan Boujon 2023-10-02 22:59:51 +02:00
parent 0779c5261b
commit 88c6f71ec1
2 changed files with 54 additions and 10 deletions

View file

@ -19,10 +19,11 @@ 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);
Vector<T>& operator()(const Vector<T>& other);
T operator[](const int indice);
T& operator[](const int indice);
Vector<T> operator+(const Vector<T>& other);
Vector<T>& operator+=(const Vector<T>& other);
private:
size_t _capacity;
@ -141,7 +142,7 @@ std::istream& operator>>(std::istream& stream, Vector<T>& vec)
}
template <class T>
T Vector<T>::operator[](const int indice)
T& Vector<T>::operator[](const int indice)
{
if(indice > _size)
throw "Indice from the vector is not reachable";
@ -149,4 +150,32 @@ T Vector<T>::operator[](const int indice)
return _values[indice];
}
template <class T>
Vector<T> Vector<T>::operator+(const Vector<T>& other)
{
if(_size != other._size)
throw "Cannot add two vectors with different sizes together";
else
{
Vector<T> returnVector(_size);
returnVector._size = _size;
for(auto i =0; i < _size; i++)
returnVector._values[i] = _values[i] + other._values[i];
return returnVector;
}
}
template <class T>
Vector<T>& Vector<T>::operator+=(const Vector<T>& other)
{
if(_size != other._size)
throw "Cannot add two vectors with different sizes together";
else
{
for(auto i =0; i < _size; i++)
_values[i] += other._values[i];
return *this;
}
}
#endif // HEADER_VECTOR

View file

@ -1,21 +1,36 @@
#include <iostream>
#include "../include/vector.h"
#include <iostream>
int main(void)
{
Vector<int> intVector;
for(int i=0; i<6; i++)
{
for (int i = 0; i < 6; i++) {
intVector.PushBack(i);
std::cout << intVector[i] << std::endl;
}
try{
try {
std::cout << intVector[12] << std::endl;
} catch (const char* expr) {
std::cout << "Error: " << expr << std::endl;
}
catch(const char* expr)
{
intVector[2] = 21;
std::cout << intVector << std::endl;
Vector<int> secondVector(intVector.Size(), 5), tooLargeVector(3, 10);
std::cout << secondVector + intVector << std::endl;
try {
std::cout << tooLargeVector + intVector << std::endl;
} catch (const char* expr) {
std::cout << "Error: " << expr << std::endl;
}
intVector+=secondVector;
std::cout << intVector << std::endl;
try {
tooLargeVector += intVector;
} catch (const char* expr) {
std::cout << "Error: " << expr << std::endl;
}
return 0;
}