简体   繁体   中英

Error assignment of read-only location '__result.std::_Rb_tree_const_iterator<_Tp>::operator*<long long int>()'

Some error while doing xor to all elements of a std::set . below is partial code. Don't know much about std::transform . help pls :)

    #include<bits/stdc++.h>
    #define ll long long int 

    using namespace std;

    int main()
    {
       set<ll> e1 ;//suppose i had inserted few elements in it!
       ll x2;
       cin>>x2;
       //now i want to xor all elements of set with x2.
       std::transform(std::begin(e1), std::end(e1), std::begin(e1), [=](ll x){return x2^x;});
       return 0;
     }

Error message:

Error assignment of read-only location '__result.std::_Rb_tree_const_iterator<_Tp>::operator*()'

You can't. A std::set doesn't allow in-place modification of its elements, so its iterators can never be written to. Thus, you can't use a std::set as the target of a std::transform .

You'd have to create a new set from the elements of the old, transforming as you go. (For example, by using a std::inserter as the target of the std::transform .)

Are you sure you can't just use std::vector ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM