列表迭代器 +操作员
// ((++currentEntry)--) is equivalent to (currentEntry + 1). Kind of.
menuEntries.insert((++currentEntry)--, newEntries.begin(), newEntries.end());
所以我这里有世界上最糟糕的代码。有更好的方法吗?
当使用“+ 1”时,我得到这个:
source/menu.cpp:146:37: error: invalid operands to binary expression
('list<menuEntry *>::iterator' (aka '_List_iterator<menuEntry *>') and
'int')
menuEntries.insert(currentEntry + 1, ...
~~~~~~~~~~~~ ^ ~
// ((++currentEntry)--) is equivalent to (currentEntry + 1). Kind of.
menuEntries.insert((++currentEntry)--, newEntries.begin(), newEntries.end());
So I have the world's worst piece of code here. Is there a better way to do this?
When using '+ 1' I get this:
source/menu.cpp:146:37: error: invalid operands to binary expression
('list<menuEntry *>::iterator' (aka '_List_iterator<menuEntry *>') and
'int')
menuEntries.insert(currentEntry + 1, ...
~~~~~~~~~~~~ ^ ~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么不分成多行:
其中
iterator
是列表的迭代器类型。就我个人而言,为了进一步清晰起见,我可能会将 ++nextEntry 拉到自己的行上 - 但这可能是一个主观决定。Why not split into multiple lines:
where
iterator
is iterator type for the list. Personally, I would probably pull the++nextEntry
onto its own line for further clarity - but that is probably a subjective decision.辅助函数怎么样:
然后您可以将
(++currentEntry)--
替换为Next(currentEntry)
编辑:
甚至更好:如果您使用 Boost,请参阅 Rob 的建议 下一步和实用程序库中的优先级
How about a helper function:
You could then replace
(++currentEntry)--
byNext(currentEntry)
Edit:
Or even better: If you use Boost see Rob's suggestion of next and prior in the Utility library
或者
or
您可以使用反向迭代器,例如:
将新项目添加到末尾。
当然,这是否适合您取决于您如何使用迭代器。
You can use reverse iterators, for example:
would add the new items to the end.
Of course, whether this works for you depends on how you are using your iterator.
由于您不想影响 currentEntry 迭代器,并且希望在 currentEntry 之后插入成员,因此 currentEntry + 1 是最佳选择。
Since you don't want to affect your currentEntry iterator, and you want to insert the member after currentEntry, currentEntry + 1 is the best bet.