c++ - 增量、前增量和后增量

标签 c++ increment post-increment pre-increment

请帮我解决这个问题。该表达式之后的步骤是:

//Expression
offSpring1[m1++] = temp1;

//Steps:

1.- increment m1

2.- assign temp1 to offSpring

一直以为括号内的表达式是第一个做的。但是现在我很困惑。所以如果这样写:

//Expression
offSpring1[++m1] = temp1;
//Steps would be:

1.- assign temp1 to offSpring
2.- increment m1

如果步骤与第一个步骤相同,那么 i++ 和++i 之间有什么区别?

最佳答案

int i = 0;
std::cout << i++ << std::endl;
std::cout << i << "\nreset" << std::endl;
i = 0;
std::cout << ++i << std::endl;
std::cout << i << std::endl;

输出:

0
1
reset
1
1

i++ 返回表达式中当前的值,然后递增变量。 ++i 将递增变量,然后返回要在当前表达式中使用的值。

关于c++ - 增量、前增量和后增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9298020/

相关文章:

web-services - Web 服务返回唯一的自动递增的人类可读 ID 号

c - 这是 C 中未定义的行为吗?

c - 为什么++不能正确递增?

c++ - 在 C++ 中是否有任何统一的方法来创建自定义比较器?

php - 如何编写投票系统?

c++ - 如何在 Microsoft Visual C++ 中导入 dllimport

Python整数用++递增

java - Java 后递增器的行为

c++ - Tizen Construct() Button 方法失败

c++ - 从标准容器迭代器派生