我一直在寻找答案,但我越来越困惑。

我有这两个for循环

for (int i = 1; (i < 5) && move.from[i - 1]; i++) {
    int const departurePoint = move.from[i - 1];
    int arrivalPoint = move.to[i - 1];

    if (arrivalPoint < 0) { // A blot was hit
        arrivalPoint = -arrivalPoint;
        board[1 - turn][BAR - arrivalPoint]--; // Remove the blot
        board[1 - turn][BAR]++; // and place it on the bar */
    }

    board[turn][departurePoint]--; // Move our own checker
    board[turn][arrivalPoint]++; // to it's landing spot.
}




for (int i = 1; (i < 5) && move.from[i - 1]; ++i) {
    int const departurePoint = move.from[i - 1];
    int arrivalPoint = move.to[i - 1];

    if (arrivalPoint < 0) { // We hit a blot
        arrivalPoint = -arrivalPoint;
        board[1 - turn][BAR - arrivalPoint]++; // Replace the blot
        board[1 - turn][BAR]--; // remove it from the bar
    }

    board[turn][departurePoint]++; // Replace our own checker
    board[turn][arrivalPoint]--; // to it's original spot.
}


我的问题是:


在带预增值的for循环语句中,当对“ move.from [i-1]求值时,我是否已增值?
我是否已增加陈述的正文?

最佳答案

您的简短问题是i++++i的区别是什么?

i++是增量之前的i值。 ++i的值是增量后的i的值。

例:

int i = 2;
std::cout << i++ << std::cout; // shows 2
std::cout << i << std::cout; // shows 3

i = 2;
std::cout << ++i << std::cout; // shows 3
std::cout << i << std::cout; // shows 3


i----i运算符的工作方式相同。

关于c++ - C++用于循环后增量预增量差异,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47233952/

10-13 08:27