C++ Iterator dereference and prefix increment/decrement style? Is *--Iter ok style wise?

久未见 提交于 2019-12-20 02:52:18

问题


In coding with C++ iterators if you wanted to get the previous value to what an iterator points to would you write:

*--Iter

or would you think it better to add parentheses like so:

*(--Iter)

?


回答1:


It doesn't matter as far as program correctness is concerned. But I always express this as *(--Iter) because this is more self-documenting and easier to understand to human beings than *--Iter.




回答2:


I would use the latter for clarity, but both are completely valid and equivalent.

Note: In case there's any confusion, that syntax doesn't just get the value of the predecessor iterator, it moves the iterator itself backwards. If you want to get the value of the predecessor iterator without modifying the one you have then you need a temporary.




回答3:


Even if you get one of these to work as you expect, they're both hard to read. Does this have to be done in a single statement?




回答4:


Either is fine. Use the one preferred by any coding standard you need to obey, or the one you prefer personally.



来源:https://stackoverflow.com/questions/4299701/c-iterator-dereference-and-prefix-increment-decrement-style-is-iter-ok-sty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!