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

后端 未结 4 1612
[愿得一人]
[愿得一人] 2021-01-22 22:56

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

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-22 23:34

    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.

提交回复
热议问题