c++ - unordered_set::remove_if(): C3892: 无法分配给常量变量

标签 c++

我有这样的代码:

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::setstd::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/

相关文章:

c++ - 使用 CryptoAPI 导出为 PEM 格式

C++ 多线程 : Do I need mutex for constructor and destructor?

c++ - 调用 Void Pointer Buffer 参数会引发运行时错误

c++ - 基于继承关系的模板友情

c++ - std::shuffle 的使用和效用?

c++ - 我可以在目录和子目录中搜索头文件吗?

java - 编码无符号整数

c++ - boost: vector256.hpp: 没有这样的文件或目录

c++ - 函数返回一个空 vector

c++ - 这些陈述之间有什么区别