【发布时间】:2012-04-29 00:06:09
【问题描述】:
for (Something something : setOfSomething) // OK
for (Something const& something : setOfSomething) // OK
for (Something& something : setOfSomething) // ERROR
error: invalid initialization of reference of type 'Something&'
from expression of type 'const Something'
迭代器什么时候返回const Something?它应该返回Something& 或Something const&。由于基于范围的“for”循环被解释为that,所以我对正在发生的事情没有合理的解释。
编辑:我说的是unordered_set而不是set,很抱歉造成这种混乱。
【问题讨论】:
标签: c++ stl reference for-loop iterator