Search code examples
c++setimmutabilitymutability

How to increase element in a set?


I'm trying to decrease the largest numbers until I run out of m to decrease. For that I thought the set was the best solution, so I tried it. It didn't work. This is the first time I run into an error like this. Is there any way to change elements "mutability". If you have any advice for a better solution feel free to answer.

set<pair<float, long int>> t;
    long unsigned n, m;
    scanf("%lu%lu", &n, &m);
    for (long unsigned i = 0; i < n; i++)
    {
        float p;
        scanf("%f", &p);
        t.insert({p, 1});
    }
    m -= n;
    while (m)
    {
        (*--t.end()).second++;
        (*--t.end()).first *= ((*--t.end()).second - 1) / (*--t.end()).second;
        m--;
    }

Solution

  • Is there any way to change elements "mutability"

    Not the elements of a set. They are always const. You may not modify them.

    What you can do instead is make a copy of the element, erase the element from the set, and insert a modified value.

    P.S. Instead of (*--t.end()), use t.back()