7

Possible Duplicates:
Why use ++i instead of i++ in cases where the value is not used anywhere else in the statement?
Incrementing in C++ - When to use x++ or ++x?

i++ vs. ++i  

When is this used in real scenarios?

Community
  • 1
  • 1
Srikar Doddi
  • 15,499
  • 15
  • 65
  • 106
  • 1
    This is a duplicate of a lot of questions, including: http://stackoverflow.com/questions/24886/ http://stackoverflow.com/questions/467322/ http://stackoverflow.com/questions/1907140/ http://stackoverflow.com/questions/484462/ http://stackoverflow.com/questions/467322/ http://stackoverflow.com/questions/24853/ – George Stocker Aug 24 '10 at 00:59
  • This is not a duplicate as I am asking for real examples. Please see RC's answer below. – Srikar Doddi Aug 24 '10 at 01:00
  • 1
    Continued: http://stackoverflow.com/questions/561588/what-is-more-efficient-i-or-i http://stackoverflow.com/questions/24901/ http://stackoverflow.com/questions/53455/ http://stackoverflow.com/questions/1116735/i-less-efficient-than-i-how-to-show-this http://stackoverflow.com/questions/3072421/difference-between-i-and-i http://stackoverflow.com/questions/2315705/what-is-the-difference-between-i-i-in-for-loop-java – George Stocker Aug 24 '10 at 01:00
  • @George Most of your links are about performance. – Srikar Doddi Aug 24 '10 at 01:01
  • 1
    @codetoglory I was just getting started, here are a few questions that have asked exactly what you've asked, using the same verbiage: http://stackoverflow.com/questions/1812990/incrementing-in-c-when-to-use-x-or-x http://stackoverflow.com/questions/2513227/whether-a-language-needs-preincrement-x-and-postincrement-x http://stackoverflow.com/questions/631506/does-pre-and-post-increment-decrement-operators-in-c-have-same-performance-in-a http://stackoverflow.com/questions/2989704/what-is-the-difference-between-pre-increment-and-post-increment-of-a-function-arg – George Stocker Aug 24 '10 at 01:04
  • these are just when to use what..I am asking for examples of each.. – Srikar Doddi Aug 24 '10 at 01:07
  • @SrikarDoddi what's the difference between *when to use what* and *examples (When is this used in real scenarios)*? – nawfal Jul 20 '14 at 07:47

3 Answers3

6

The obvious is when you want the old value returned, you use post-increment.

The more subtle things are that pre-increment should really never be slower and could be faster due to the lack of creating a temporary and returning the old value when using post-increment.

A real scenario for using post-increment in C++ is when erasing from standard containers. For example:

set<int> ctr;
ctr.insert(1);
ctr.insert(10);
ctr.insert(12);
ctr.insert(15);

set<int>::iterator it = set.begin();

// Post-increment so the value returned to erase is that of the previous
// iteration (i.e. begin()), yet the iterator stays valid due to the local
// iterator being incremented prior to the erase call
ctr.erase(it++);

// Still valid iterator can be used.
cout << "Value: " << *it << "\n";  

In response to the compiler optimization, it is true yet I think it's always important to convey as precisely as possible what you're trying to accomplish. If you don't need the returned value from x++, then don't ask for it. Also, I'm not sure you would always get the same optimization if the type your incrementing is not a simple type. Think iterators that are not just plain pointers. In cases such as this, your mileage may vary with regard to optimization. In short, always pre-increment unless you need the returned (i.e. old value) of the post-increment operator.

RC.
  • 27,409
  • 9
  • 73
  • 93
  • +1 for the subtle optimization tip. – David Young Aug 24 '10 at 00:52
  • 1
    Actually, I think almost every compiler will optimize `i++` to `++i` under the usual circumstances (where the result of the operation is ignored and `i` has no overloaded increment/decrement operators). That said, I still prefer to pre-increment/decrement instead of post-incrementing/decrementing, as I think it convoys the meaning of the action better. And you likely *will* see a speed up if `i` does happen to be a user-defined type with custom `++`/`--` operators. – bcat Aug 24 '10 at 00:53
  • @bcat agree with you..in most cases it just makes sense to pre-increment, not sure why this is such a big deal when they teach at schools. – Srikar Doddi Aug 24 '10 at 00:58
  • @CodeToGlory: I don't know. At my school all the CS professors use post-increments, even in for-loops. It drives me kind of insane, to be honest. :) – bcat Aug 24 '10 at 01:01
  • I've updated my response for bcat's comment – RC. Aug 24 '10 at 01:01
  • 1
    This is interesting given how much may go on with a "simple" `++/--` in C++, but when you're using plain int, especially in C (not C++) the difference is negligible, and *especially* with PDP/VAX-11 architecture where register pre/post-increment/decrement are directly supported, and I'd guess that's why the CS profs use post-increments. – Stephen P Aug 24 '10 at 01:12
  • For me it's a legibility thing, `i++` is almost always more readable; see my response – zwol Aug 24 '10 at 01:42
  • @maciej - I see your point where my statement about STD containers could be misinterpreted. To clarify std::set, it does invalidate the iterator being erased and thus post increment above must be used or a later use of the iterator would lead to undefined behavior. I believe this type of example is what the OP was looking for. – RC. Aug 24 '10 at 05:04
  • @Stephen P: I hadn't thought of that, but it makes a lot of sense. I know all my profs have worked on their fair share of older hardware, so it stands to reason that it would heavily influence their coding style. – bcat Aug 24 '10 at 12:33
6

The best explanation in order to clarify the differences is:

  • i++ - use the value of i for whatever value it holds, then take the value of i, increment it, and store it
  • ++i - increment the value of i and store it immediately then use that value.

That is the crucial difference one increments AFTER the statement is evaluated, the other is incremented BEFORE the statement is evaluated.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • You had the easiest to read answer, there's a real lack of plain english on Stackoverflow unfortunately. – MarcusJ Jun 16 '15 at 23:36
5

I prefer to use i++ unless I specifically need the behavior of ++i, because that puts the variable on the left, consistent with i += N. Mostly this is minor, but in for-loops, I consider ++i to be wrong and will change it to i++ en passant, because

for (i = 0; i < limit; ++i)

trips the reader up with lack of parallel structure;

for (i = 0; i < limit; i++)

is much nicer to read, because the loop variable is in the same position in all three clauses.

Arguments from performance are hogwash. Any compiler worth its salt will generate exactly the same code in cases where the difference doesn't matter. Yes, that includes cases involving C++ overloaded operators.

zwol
  • 135,547
  • 38
  • 252
  • 361