开发者

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

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开发者_如何学C:

*(--Iter)

?


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.


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.


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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜