简体   繁体   English

只读位置&#39;__result.std :: _ Rb_tree_const_iterator &lt;_Tp&gt; :: operator *的错误分配 <long long int> ()”

[英]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 . std::set所有元素进行异或运算时会出错。 below is partial code. 下面是部分代码。 Don't know much about std::transform . 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*()' 只读位置'__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. std::set不允许对其元素进行就地修改,因此永远不能写入其迭代器。 Thus, you can't use a std::set as the target of a std::transform . 因此,您不能将std::set用作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 .) (例如,通过使用std::inserter作为std::transform的目标。)

Are you sure you can't just use std::vector ? 您确定不能只使用std::vector吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自std :: _ Rb_tree_const_iterator的分段错误 <Type> ::运算++ - Segmentation fault from std::_Rb_tree_const_iterator<Type>::operator++ C ++ [错误]分配只读位置&#39;*(a +((sizetype)((((long long unsigned int)min)* 4ull)))&#39;&#39; - C++ [Error] assignment of read-only location '*(a + ((sizetype)(((long long unsigned int)min) * 4ull)))' 返回迭代器时,“对std :: _ Rb_tree_const_iterator的未定义引用” - “undefined reference to std::_Rb_tree_const_iterator” when returning iterator 错误:只读位置“* __result”的分配 - error: assignment of read-only location ‘* __result’ 反向迭代器错误:&#39;rcit!= std :: vector &lt;_Tp,_Alloc&gt; :: rend()中的&#39;operator!=&#39;与_Tp = int,_Alloc = std :: allocator&#39;不匹配 - reverse iterator error : no match for 'operator!=' in 'rcit != std::vector<_Tp, _Alloc>::rend() with _Tp = int, _Alloc = std::allocator' 非const变量的只读位置的分配 - assignment of read-only location of non const variable 我遇到了不转换 const__gnu_cxx::__normal_iterator 的问题<long long int* , std:: vector<long long int> - I'm having an issue with not converting const__gnu_cxx::__normal_iterator<long long int* , std:: vector<long long int> 错误:分配只读位置&#39;arr2.IntArray :: operator [](1)&#39;arr2 [1] = 24; - error: assignment of read-only location ‘arr2.IntArray::operator[](1)’ arr2[1] = 24; std :: swap只读参考的错误分配 - std::swap error assignment of read-only reference 错误:分配只读位置<unnamed> :: g_namesmap - error: assignment of read-only location <unnamed>::g_namesmap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM