[英]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.