TD2 Finished.
This commit is contained in:
parent
0779c5261b
commit
88c6f71ec1
2 changed files with 54 additions and 10 deletions
|
@ -19,10 +19,11 @@ public:
|
||||||
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);
|
Vector<T>& operator=(const Vector<T>& other);
|
||||||
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:
|
private:
|
||||||
size_t _capacity;
|
size_t _capacity;
|
||||||
|
@ -141,7 +142,7 @@ std::istream& operator>>(std::istream& stream, Vector<T>& vec)
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
T Vector<T>::operator[](const int indice)
|
T& Vector<T>::operator[](const int indice)
|
||||||
{
|
{
|
||||||
if(indice > _size)
|
if(indice > _size)
|
||||||
throw "Indice from the vector is not reachable";
|
throw "Indice from the vector is not reachable";
|
||||||
|
@ -149,4 +150,32 @@ T Vector<T>::operator[](const int indice)
|
||||||
return _values[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
|
#endif // HEADER_VECTOR
|
|
@ -1,21 +1,36 @@
|
||||||
#include <iostream>
|
|
||||||
#include "../include/vector.h"
|
#include "../include/vector.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
Vector<int> intVector;
|
Vector<int> intVector;
|
||||||
for(int i=0; i<6; i++)
|
for (int i = 0; i < 6; i++) {
|
||||||
{
|
|
||||||
intVector.PushBack(i);
|
intVector.PushBack(i);
|
||||||
std::cout << intVector[i] << std::endl;
|
std::cout << intVector[i] << std::endl;
|
||||||
}
|
}
|
||||||
try{
|
|
||||||
|
try {
|
||||||
std::cout << intVector[12] << std::endl;
|
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;
|
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;
|
return 0;
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue