没有匹配“ operator + =”的aka std :: _ Rb_tree_const_iterator std :: map [英] no match for ‘operator+=’ aka std::_Rb_tree_const_iterator std::map

查看:154
本文介绍了没有匹配“ operator + =”的aka std :: _ Rb_tree_const_iterator std :: map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆