我有这样的代码:
unordered_set<AttrValue> output;
...
auto requiredType = variables.at(arg.value);
auto end = remove_if(output.begin(), output.end(),
[&](AttrValue x) {
return !matchingOutputType(requiredType, ast->getNodeType(ast->getNodeKeyAttribute(x)));
}); // queryevaluator_getcandidatelist.cpp(179)
output.erase(end);
错误在代码的第 4 行。所以我认为这是因为 remove_if
。但是怎么了?输出没有定义常量?
Error 90 error C3892: '_Next' : you cannot assign to a variable that is const c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm 1840
Error 109 error C3892: '_Next' : you cannot assign to a variable that is const c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm 1840
输出窗口:
3>c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(1840): error C3892: '_Next' : you cannot assign to a variable that is const
3> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(1853) : see reference to function template instantiation '_FwdIt std::_Remove_if<std::_List_unchecked_const_iterator<_Mylist>,_Pr>(_FwdIt,_FwdIt,_Pr)' being compiled
3> with
3> [
3> _FwdIt=std::_List_unchecked_const_iterator<std::_List_val<int,std::allocator<AttrValue>>>,
3> _Mylist=std::_List_val<int,std::allocator<AttrValue>>,
3> _Pr=`anonymous-namespace'::<lambda4>
3> ]
3> h:\dropbox\sch\cs3202\code\source\query\query evaluator\queryevaluator_getcandidatelist.cpp(179) : see reference to function template instantiation '_FwdIt std::remove_if<std::_List_const_iterator<_Mylist>,`anonymous-namespace'::<lambda4>>(_FwdIt,_FwdIt,_Pr)' being compiled
3> with
3> [
3> _FwdIt=std::_List_const_iterator<std::_List_val<int,std::allocator<AttrValue>>>,
3> _Mylist=std::_List_val<int,std::allocator<AttrValue>>,
3> _Pr=`anonymous-namespace'::<lambda4>
3> ]
最佳答案
根据标准 § 23.2.4.6
For associative containers where the value type is the same as the key type, both iterator and const_iterator are constant iterators.
所以,你甚至做不到
std::unordered_set<int> s{1, 2, 3, 4};
*s.begin() = 42;
当然,您不能使用 std::remove_if(ForwardIt first, ForwardIt last, ...)
函数从 std::set
和 std::unordered_set
:
The type of dereferenced ForwardIt must meet the requirements of MoveAssignable.
关于c++ - unordered_set::remove_if(): C3892: 无法分配给常量变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15716665/