问题描述
我有一个从const函数返回的名为 assets
的地图,我使用const_iterator来获取地图的子集,如下所示:
I have a map called assets
returned from a const function, I use a const_iterator to get a subset of the map like this:
std::map<int, Asset>::const_iterator start = assets.begin();
start += 5;
......
但是我得到了错误:错误:'operator + ='不匹配(操作数类型为'std :: map< int,Asset> :: const_iterator {aka std :: _ Rb_tree_const_iterator< std :: pair< const int,Asset>}}和'int ')
推荐答案
这是因为 std :: map
迭代器是BidirectionalIterators,不是RandomAccessIterators-因此支持 operator ++
和 operator-
但不支持 operator + =
和 operator-=
。
This is because std::map
iterators are BidirectionalIterators, not RandomAccessIterators - and hence support operator++
and operator--
but not operator+=
and operator-=
.
使用 std :: advance(start,5)
(请记住,这将导致重复调用 operator ++
)。
Use std::advance(start, 5)
instead (bearing in mind that this will result in repeated invocation of operator++
).
这篇关于没有匹配“ operator + =”的aka std :: _ Rb_tree_const_iterator std :: map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!