问题
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