2

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)

?

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
Paul Caheny
  • 1,281
  • 3
  • 11
  • 16
  • 1
    In order to just get the previous value, I would personally prefer the nondestructive `*(Iter-1)`. – Vlad Nov 28 '10 at 23:23
  • Vlad - I didn't think of that option as in my particular case at the moment the Iterator is not used again after the decrement and dereference. Given that case - would you still prefer your suggestion over the two in the original question? – Paul Caheny Nov 28 '10 at 23:31
  • @Czarak: yes. This would show the clear intention to get the previous value, and not to change the iterator's position. Changing the iterator's position hints to the reader that the new value is going to be used later. – Vlad Nov 28 '10 at 23:39
  • 2
    @Vlad: That would only work for random access iterators, although you could use the `prev()` function from C++0x / Boost. – UncleBens Nov 28 '10 at 23:44
  • @UncleBens: yes. At least, `vector`'s iterator has this functionality. This won't compile with `map` (at least on MSVC's STL implementation). – Vlad Nov 28 '10 at 23:50

4 Answers4

7

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.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • The extra two characters, although redundant, do make it much more understandable especially to those that do not know precedence rules inside-out. – dreamlax Nov 28 '10 at 23:22
  • 4
    Operator precedence is important if an operand is surrounded by two different operators. This is not the case here, so **operator precedence does not matter at all in this example**. Even if `*` had a higher precedence than `--` (I do not know by heart if this is the case or not), `--` still binds to `Iter` because there is no contention. `Iter` is not an operand of `*`. If it was about operator precedence, how would you solve it? `(*--)Iter`? There is no solution, because there is no problem. `Iter` has only one operator fixed to it, not two. – fredoverflow Nov 28 '10 at 23:38
  • In fact, if you [look it up](http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B), `*` and prefix `--` have the *exact same* operator precedence. (And if you don't trust Wikipedia, TC++PL page 120 agrees.) Again, not that it really matters, but the statement "`--` has higher precedence than dereference" is **wrong**. – fredoverflow Nov 28 '10 at 23:49
  • @Fred: All of which is completely irrelevant to my point. But if it makes you feel better, I'll edit my post. – John Dibling Nov 28 '10 at 23:53
  • @Fred, good point. So purely from a style perspective, which would you prefer *(--Iter) or *--Iter? – Paul Caheny Nov 28 '10 at 23:58
  • 1
    @Czarak: I prefer `*--Iter` because the added parenthesis cannot possibly change the meaning, so they are just visual clutter. – fredoverflow Nov 29 '10 at 00:01
0

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.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
  • Nope, don't need the iterator after the dereference. – Paul Caheny Nov 28 '10 at 23:22
  • Peter - good suggestion by Vlad in the comment on the question above to get the value of the predecessor iterator without modifying the one I have and without using a temporary. – Paul Caheny Nov 28 '10 at 23:33
0

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?

robert
  • 33,242
  • 8
  • 53
  • 74
  • No, it doesn't have to be a single statement.How do you mean if I get one of those to work as I expect? They both do what I expect and they both do the same thing? – Paul Caheny Nov 28 '10 at 23:23
  • 1
    I disagree that either is hard to read. Professional programmers won't be thrown by this, though rookies may be. – John Dibling Nov 28 '10 at 23:27
  • 1
    @John It stinks of "clever" to me. I try to make my C++ code look as little like Perl as possible. – robert Nov 28 '10 at 23:29
0

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

Håvard S
  • 23,244
  • 8
  • 61
  • 72