没有匹配“ operator + =”的aka std ::

编程入门 行业动态 更新时间:2024-10-09 15:24:48
本文介绍了没有匹配“ 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 ::

本文发布于:2023-11-24 16:56:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626062.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:operator   aka   std

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!