0

I am getting this error on compiling :

prog.cpp:40:17: error: assignment of read-only location ‘p.std::_Rb_tree_const_iterator<_Tp>::operator*<int>()’
           *p=-1;  

It has something to do with assignment of a numerical constant to an iterator. Any other way in which I can assign some value to an iterator to update it.

CODE:

#include <bits/stdc++.h>
using namespace std;

int main()
{

    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n;

        int arr[n];

        multiset<int> m;

        for (int i = 0; i < n; i++) {
            cin >> arr[i];
            m.insert(arr[i]);
        }

        int sum = 0;
        for (auto it = m.rbegin(); it != m.rend(); it++) {
            if (*it > 0) {
                int x = *it;
                sum += x;
                auto p = m.find(x - 1);

                if (p != m.end()) {
                    *p = -1;
                }
            }
        }

        cout << sum << endl;
    }

    return 0;
}
Brian61354270
  • 8,690
  • 4
  • 21
  • 43
  • 5
    (multi)`set` "keys" should not be modified, you have to remove it, and re-add it (care with iterator invalidation) – Jarod42 Sep 03 '20 at 14:57
  • 1
    You may also want to read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Brian61354270 Sep 03 '20 at 15:08

0 Answers0