Untitled
unknown
c_cpp
4 years ago
2.8 kB
11
Indexable
//SOMMA CON CONTENITORE DINAMICO PUO' ESSERE FATTA SOLO PASSANDO UN PUNTATORE ALLA INLINE E POI RIPASSANDOGLIELO SU UN' ALTRA FUNZIONE
//OVVIAMENTE NON SI PUO' OPERARE CON CONTENITORI AVENTI TIPI DI VARIABILI DIVERSI TRA DI LORO
#include <iostream>
using namespace std;
#define DEFAULT_CAPACITY 50
template <class T>
class DinamicContainer {
public:
//Costruttore
DinamicContainer(int firstCapacity = DEFAULT_CAPACITY);
//Distruttore
~DinamicContainer() {
delete[] elements;
}
//Funzioni/procedure
int getUsedValues();
int getCapacity();
int getSize(DinamicContainer<T>& firstIstance, DinamicContainer<T>& secondIstance); //Necessaria per l' inline con l' operator +
T getElements(int index);
void add(T value);
int count(T value);
bool removeOne(T value);
int removeAll(T value);
void setDinamicSize(int value);
void istancesSum(DinamicContainer<T>& firstIstance, DinamicContainer<T>& secondIstance, DinamicContainer<T>& thirdIstance);
void operator +=(DinamicContainer<T> istance);
void operator =(DinamicContainer<T> istance);
private:
T* elements;
int usedValues;
int capacity;
};
//OVERLOADING OPERATORI
/*Essendo che stiamo allocando tutto in dinamico, e stiamo creando un nuovo contenitore sempre in dinamico, per evitare di
sovraccaricare il buffer e di avere problemi con lo heap, passiamo tutto per un puntatore
QUESTA E' LA FUNZIONE D' APPOGGIO CHE USEREMO CON void istancesSum*/
template <class T>
inline T* operator + (DinamicContainer<T>& firstIstance, DinamicContainer<T>& secondIstance) { //&OBBLIGATORIE
int i = 0;
int j = 0;
int values = 0;
T* p;
if (firstIstance.getUsedValues() >= secondIstance.getUsedValues())
p = new T[firstIstance.getUsedValues()];
else
p = new T[secondIstance.getUsedValues()];
while (i < firstIstance.getUsedValues() || j < secondIstance.getUsedValues()) { //Fino all' ultimo elemento utilizzato per ogni contenitore
/*E' importantissimo rompere il ciclo quando arrivo alla fine per evitare che in un contenitore ho 11 elementi e nell' altro 10 e si provi a
sommare l' 11 elemento del contenitore con un elemento non inizalizzato del secondo*/
p[values] = (firstIstance.getElements(i) + secondIstance.getElements(j));
values++;
i++;
j++;
if (i == firstIstance.getUsedValues() || j == secondIstance.getUsedValues())
break;
}
return p;
}
template <class T>
inline ostream& operator << (ostream& out, DinamicContainer<T>& istance) {
for (int i = 0; i < istance.getUsedValues(); i++)
out << istance.getElements(i) << " ";
cout << endl;
return out;
}
#include "ContenitoreDinamico.tpp"Editor is loading...