Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template <class T>
- class Array
- {
- private:
- int size;
- T *arr;
- int32_t countSetBits(uint8_t value)
- {
- if (value == 0)
- return 0;
- else
- return (value & 1) + countSetBits(value >> 1);
- }
- int32_t countOverByteArray(uint8_t* array, size_t size)
- {
- int32_t result = 0;
- for (size_t i = 0; i < size; ++i)
- result += countSetBits(array[i]);
- return result;
- }
- public:
- Array<T>(size_t size)
- {
- arr = new T[this->size = size];
- if (!arr)
- {
- cout << "Пам'ять не виділено!" << endl;
- return;
- }
- cout << "Створено!" << endl;
- }
- ~Array<T>()
- {
- cout << "Видалено!" << endl;
- delete[] arr;
- }
- int CountOfDigit()
- {
- return countOverByteArray(reinterpret_cast<uint8_t*>(arr), size * sizeof(T));
- }
- friend class ArrayIterator<T>;
- friend ostream &operator << <>(ostream&, const Array&);
- friend istream &operator >> <>(istream&, Array&);
- };
- template <class T>
- ostream &operator << (ostream &out, Array<T> const &mas)
- {
- if (sizeof(T) == 1)
- for (size_t i = 0; i < mas.size; i++)
- out << mas.arr[i];
- else
- for (size_t i = 0; i < mas.size; i++)
- out << mas.arr[i] << ' ';
- return out;
- }
- template <class T>
- istream &operator >> (istream &in, Array<T> &mas)
- {
- for (size_t i = 0; i < mas.size; i++)
- in >> mas.arr[i];
- return in;
- }
- template <class T>
- class ArrayIterator
- {
- private:
- const Array<T>& array;
- int index = 0;
- public:
- ArrayIterator(const Array<T> &prg) : array(prg)
- {
- index = 0;
- }
- void operator++(int)
- {
- index++;
- }
- bool operator()()
- {
- return index != Array.number;
- }
- Array<T>& operator *()
- {
- return *array.arr[index];
- }
- };
- int main(void)
- {
- system("color 70 & chcp 1251 & cls");
- int ArraySize;
- cout << "Введіть розмір масиву -> ";
- cin >> ArraySize;
- Array<int> arr(ArraySize);
- ArrayIterator<int> ArrIT(arr);
- cout << "Введіть елементи масиву -> ";
- cin >> arr;
- cout << "Кількість одиниць" << endl;
- cout << arr.CountOfDigit() << endl;
- //cout << " Введені елементи: " << arr << endl;
- while (ArrIT())
- {
- cout << *ArrIT << endl;
- ArrIT++;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment