Advertisement
Pug_coder

lab10

May 12th, 2021
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include "vector"
  3. using namespace std;
  4. class IntArray
  5. {
  6. private:
  7.     int k, n;
  8.     vector<int> m_array;
  9.     vector<int> sum;
  10. public:
  11.     IntArray(int n, int k){
  12.         this->k = k;
  13.         this->n = n;
  14.         this->m_array.resize(n);
  15.     }
  16.     int operator[] (int index);
  17.     vector<int>::iterator begin() {
  18.         return this->sum.begin();
  19.     };
  20.  
  21.     void set(int i, int v) {
  22.         m_array[i] = v;
  23.         findsum();
  24.     }
  25.  
  26.     vector<int>::iterator end() {
  27.         return this->sum.end();
  28.     };
  29.  
  30.     void findsum() {
  31.         this->sum.clear();
  32.         for(int i = 0; i < n - k; i++) {
  33.             int sum1 = 0;
  34.             for (int j = i; j < i + k; j++) {
  35.                 sum1 += m_array[j];
  36.             }
  37.             this->sum.push_back(sum1);
  38.         }
  39.     }
  40. };
  41.  
  42. int IntArray::operator[] (const int index)
  43. {
  44.     return m_array[index];
  45. }
  46.  
  47. int main() {
  48.     int n = 10, k = 2;
  49.     IntArray array(n, k);
  50.     //array[4] = 5;
  51.     for(int i = 0; i < n; i++) {
  52.         array.set(i, i);
  53.        // cout << array[i] << " ";
  54.     }
  55.  
  56.     for(auto i = array.begin(); i < array.end(); i++) {
  57.         cout << *i << " ";
  58.     }
  59.     cout << endl;
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement