I was taught not to write things like a=a+++++a.
However I love writing a++ because I believe that it always gets executed after a whole sentence (perhaps, right after a , or ;?)
But today I wrote this and got WA:
if(hh < tt && a[hh+1] == a[hh]-1) c[hh+1] += 2*c[hh++];
wa code: 154812779
rewrote it like this and got AC:
if(hh < tt && a[hh+1] == a[hh]-1) c[hh+1] += 2*c[hh], hh++;
ac code: 154812722
I'm wondering if it's wrong somewhere else or am I misunderstanding the usage of x++?








It's an undefined behavior in C++.
See this for more infomation.
What is "more than one modifications of the same scalar" here?
If I just modify c[hh+1] and let hh++, like
c[hh+1] += hh++, then hh will definitely be changed after c[hh+1].But in
c[hh+1] += 2*c[hh++], c[hh+1] may be changed twice, so it is possible that hh++ was done before it. Am I right?Yeah, I think so. You modified
hhand get the reference ofc[hh+1], and it is called "more than one modifications".In this code, you know first the computer should calculate
c[hh+1]('s address) andhh++, but it is not defind thatc[hh+1]orhh++which is calculated first. Becausehh++will changehh, so which one is calculated first influences the arrayc.It is undefined behavior in C++. In different computer or use different compiler it may get different output. You can use
-Wallto find undefined behavior.Thanks! I tried it and get "warning: operation on 'hh' may be undefined [-Wsequence-point]".