Search code examples
c++stlmultiset

std::transform on a multiset giving me error C3892


I am trying to understand how the std::transform function works, but I'm having a bit of trouble with the following code. I want to take a multiset ms, add 1 to the contents of each element and store them in a new multiset msc. Here is what I have:

int op_increase(int i) { return ++i; }

int main()
{

std::multiset<int> ms = {1,1,2,2,3};
std::multiset<int> msc;
std::transform(ms.begin(), ms.end(), msc.begin(), op_increase);

return 0;
}

However I get the following error:

C3892: _Dest: you cannot assign to a variable that is const


Solution

  • Your code was not utilizing the correct argument to std::transform that allows insertion into an empty container. This requires using an iterator that is intelligent enough to call the appropriate function that calls the container's insert() function.

    The solution is to provide std::transform the std::inserter iterator that automatically inserts into the empty multiset. Here is an example:

    #include <set>
    #include <algorithm>
    #include <iterator>
    
    int op_increase(int i) { return ++i; }
    
    int main()
    {
        std::multiset<int> ms = {1,1,2,2,3};
        std::multiset<int> msc;
        std::transform(ms.begin(), ms.end(), std::inserter(msc, msc.begin()), op_increase);
        // msc now contains 2,2,3,3,4  
    }
    

    Note the std::inserter is used, and not merely msc.begin(). The inserter will automatically insert the items into the map.

    Live Example