![](/img/trans.png)
[英]Segmentation fault from std::_Rb_tree_const_iterator<Type>::operator++
[英]Error assignment of read-only location '__result.std::_Rb_tree_const_iterator<_Tp>::operator*<long long int>()'
對std::set
所有元素進行異或運算時會出錯。 下面是部分代碼。 對std::transform
不太了解。 幫助請:)
#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;
}
錯誤信息:
只讀位置'__result.std :: _ Rb_tree_const_iterator <_Tp> :: operator *()的錯誤分配
你不能 std::set
不允許對其元素進行就地修改,因此永遠不能寫入其迭代器。 因此,您不能將std::set
用作std::transform
的目標。
您必須從舊元素中創建一個新集合,並隨需轉換。 (例如,通過使用std::inserter
作為std::transform
的目標。)
您確定不能只使用std::vector
嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.