0

This is a part of the code snippet for a merge sort implementation and I have trouble understanding this line:

b[bIdx++] = (a[left] <= a[right]) ? a[left++] : a[right++];

I am re-writing the above snippet to check my understanding of the line. Is my understanding of the above line correct?

if (a[left] <= a[right]){
    b[bIdx] = a[left];
    left++;
}
else { 
    b[bIdx] = a[right];
    right++;
}
bIdx ++;
Gen Tan
  • 858
  • 1
  • 11
  • 26
  • 3
    Yes. Your code is correct. The postfix `++` is evaluated after the operand’s value is used but before the next statement is evaluated. Recent versions of C++ tightened the spec to reduce UB but AFAIK/IIRC it’s still UB in some cases but in your case it’s fine. – Dai Jan 09 '20 at 03:07
  • By "next statement", do you mean the next line of the code? Or in the one liner, is `b[Bidx++]` one statement, and ``a[left++]`` another statement – Gen Tan Jan 09 '20 at 03:11
  • 1
    Does this answer your question? [What is the correct answer for cout << a++ << a;?](https://stackoverflow.com/questions/10782863/what-is-the-correct-answer-for-cout-a-a) – Ken White Jan 09 '20 at 03:14
  • Does your conversion have the same behaviour? – eerorika Jan 09 '20 at 03:15
  • Yes its correct, you understood right. –  Jan 09 '20 at 03:21
  • 1
    you should learn to do quick unit tests on small snippets of code in order to verify yourself – Bwebb Jan 09 '20 at 03:21

3 Answers3

2

Your understanding is correct.

The postfix increment operator ++ evaluates to the current value of its operand, and as a side effect increments its operand. The effect of the increment is guaranteed to be visible at the next sequence point.

A sequence point occurs after each statement, but it could occur within an expression based on the operator. Examples of operators that create a sequence point are the logical AND operator &&, the logical OR operator ||, the comma operator ,, and as in your case the ternary operator ?:.

Breaking down the single line statement, the condition a[left] <= a[right] is evaluated first. There is now a sequence point in this stage of the evaluation. Based on the result of this condition, either a[left++] or a[right++] is evaluated, and whichever is evaluated is assigned to b[bIdx++]. There is a sequence point here at the end of the statement, at which point bIdx is incremented and either left or right is incremented.

The above is reflected in the multiple statement version of the code. The condition is evaluated first, then b[bIdx] is assigned either a[left] or a[right], then either left or right are incremented, then bIdx is incremented.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

Your re-written code is equivalent to the single line of code above.

The single line uses the postfix increment operator in which the value of the expression is evaluated i.e. a[left] is evaluated with the current value of left after which the operand left is incremented by 1.

Also note that there also exists a prefix increment operator in which the operand is incremented by 1 after which the value of the expression is evaluated.

anusha
  • 171
  • 7
0

Is my understanding of the above line correct?
Correct.
even more we'll clear doubts, ensure it by

if (a[left] <= a[right]){
    b[bIdx] = a[left];
    ++left;
}
else { 



    b[bIdx] = a[right];
    ++right;
}
++bIdx;

space in else e.g. above is for code filled in if you have more condition e.g. such

b[bIdx++] = (a[left] < a[right]) ?  a[left++] : ( a[left++] == a[right] ? (++right, 1)  : a[right++] );
  • you are also correct ++right or right++ which are used independently like right++; ++right; do the same thing – Akash Jan 09 '20 at 06:21