I'm having trouble figuring out whats the difference of using them prefix and postfix.
What times would I need to use both of those operations?
I'm having trouble figuring out whats the difference of using them prefix and postfix.
What times would I need to use both of those operations?
Simple. The prefix increment/decrement operators return the incremented/decremented value, while the postfix returns the original value. That is to sayOriginally Posted by SyllableCode:int x = 5; std::cout << x++; // Will output '5'It's generally better to use the prefix, as it takes less time to resolve (it doesn't have to store the original value temporarily), unless the situation arises where you need the pre-updated value to resolve in the next operator. For instance, in the subscript operator.Code:int x = 5; std::cout << ++x; // Will output '6'Code:while(x < 10) std::cout << arry[x++];
Last edited by SlyMaelstrom; 09-03-2006 at 09:53 PM. Reason: Some gramatical adjustments
Sent from my iPadŽ
Oh, and to the OP, one thing you should learn never to do with increments:
That is, you can't have two things which both modify the same value in a single expression. Incrementing is a common cause (although function calls can do it too).Code:int x = 5; std::cout << x++ << " " << ++x; // Will output whatever the heck it feels like (the behavior is undefined).
Last edited by Cat; 09-04-2006 at 02:06 AM.
You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.
What Cat mentioned is a quirk of sequence points.
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}