Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<class T, unsigned int size> class C;
- template<class T, unsigned int size> std::ostream& operator<<(std::ostream&, const C<T, size>& c);
- template<class T, unsigned int size>
- class C
- {
- template<class T1> class MultiInfo;
- friend class MultiInfo<T>; //MultiInfo รจ amica solo quando T1=T
- friend std::ostream& operator<< <T,size> (std::ostream&, const C<T, size>& c);
- private:
- //1)
- template<class T1>
- class MultiInfo {
- public:
- T1 informazione;
- int molteplicita;
- MultiInfo() {}
- MultiInfo(T1 info, int m) : informazione(info), molteplicita(m) {}
- bool operator==(const MultiInfo& m) const {
- if (this == &m)
- return true;
- else
- return (informazione == m.informazione && molteplicita == m.molteplicita);
- }
- bool operator!=(const MultiInfo& m) const {
- if (this == &m)
- return false;
- else
- return (informazione != m.informazione && molteplicita != m.molteplicita);
- }
- };
- MultiInfo<T>* a; //2) Array di oggetti multiinfo
- public:
- //3)
- C(const T& t, int k) : a(new MultiInfo<T>[size]) {
- for (unsigned int i = 0; i < size; i++) {
- a[i] = MultiInfo<T>(t, k >= 1 ? k : 0);
- }
- }
- //4)
- C(const C& c) : a(new MultiInfo<T>[size]) {
- for (unsigned int i = 0; i < size; i++) {
- a[i] = c.a[i];
- }
- }
- C& operator=(const C& c) { //costruttore di copia profonda
- if (this != &c) {
- delete[] a;
- a = new MultiInfo<T>[size];
- for (unsigned int i = 0; i < size; i++)
- a[i] = c.a[i];
- }
- return *this;
- }
- ~C() {
- delete[] a;
- }
- //5)
- T* operator[](int k) const {
- if (k >= 0 && k < size)
- return &a[k].informazione;
- else
- return nullptr;
- }
- //6)
- int occorrenze(const T& t) const {
- int k = 0;
- for (unsigned int i = 0; i < size; i++)
- if (a[i].informazione == t)
- k += a[i].molteplicita;
- return k;
- }
- //8)
- bool operator==(const C& c) const {
- if (this == &c)
- return true;
- else
- for (unsigned int i = 0; i < size; i++)
- if (a[i] != c.a[i])
- return false;
- return true;
- }
- };
- //7)
- template<class T, unsigned int size> std::ostream& operator<<(std::ostream& os, const C<T, size>& c) {
- for (unsigned int i = 0; i < size; i++)
- os << "Informazione: " << c.a[i].informazione << " Molteplicita': " << c.a[i].molteplicita << std::endl;
- return os;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement