Untitled
librayunknown
c_cpp
5 years ago
2.5 kB
13
Indexable
#include <iostream>
using namespace std;
#define DEFAULT_CAPACITY 50
class DinamicContainer {
public:
//Costruttore
DinamicContainer(int firstCapacity = DEFAULT_CAPACITY);
//Distruttore
~DinamicContainer() {
delete[] elements;
}
//Funzioni/procedure
int getUsedValues() {
return this->usedValues;
}
int getCapacity() {
return this->capacity;
}
int getSize(DinamicContainer& firstIstance, DinamicContainer& secondIstance); //Necessaria per l' inline con l' operator +
int getElements(int index);
void add(int value);
int count(int value);
bool removeOne(int value);
int removeAll(int value);
void setDinamicSize(int value);
void istancesSum(DinamicContainer& firstIstance, DinamicContainer& secondIstance, DinamicContainer& thirdIstance);
void operator +=(DinamicContainer istance);
void operator =(DinamicContainer istance);
private:
int* 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*/
inline int* operator + (DinamicContainer& firstIstance, DinamicContainer& secondIstance) { //&OBBLIGATORIE
int i = 0;
int j = 0;
int values = 0;
int* p;
if (firstIstance.getUsedValues() >= secondIstance.getUsedValues())
p = new int[firstIstance.getUsedValues()];
else
p = new int[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;
}
inline ostream& operator << (ostream& out, DinamicContainer& istance) {
for (int i = 0; i < istance.getUsedValues(); i++)
out << istance.getElements(i) << " ";
return out;
}Editor is loading...