修改std :: multiset键时分配只读位置时出错

问题描述

我在编译时遇到此错误

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

与将数字常量分配给迭代器有关。我可以通过其他方式为迭代器分配一些值来更新它。

代码

#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;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)