使用C+中的增量运算符无法获得正确的结果+; 我在C++中使用了前缀N后缀中的增量运算符,但是我得到了不正确的结果,谁能引导我告诉我我做错了什么?)谢谢 这是我的代码:)
但我得到了结果使用C+中的增量运算符无法获得正确的结果+; 我在C++中使用了前缀N后缀中的增量运算符,但是我得到了不正确的结果,谁能引导我告诉我我做错了什么?)谢谢 这是我的代码:),c++,C++,但我得到了结果 0 2 1 2 这与增量运算符+++及其工作方式有关,具体取决于您是将其用作前缀还是后缀: #include <iostream> int main() { int a = 0; std::cout << a++ << std::endl // Output: 0. ++ as posfix creates an incremented copy of "a" and AFTER the li
0
2
1
2
这与增量
运算符+++
及其工作方式有关,具体取决于您是将其用作前缀还是后缀:
#include <iostream>
int main() {
int a = 0;
std::cout << a++ << std::endl
// Output: 0. ++ as posfix creates an incremented copy of "a" and AFTER the line gets executed.
std::cout << ++a << std::endl;
// Output: 1. ++ as prefix creates a copy of "a", an incremented copy BEFORE the line gets executed.
std::cout << a++ << std::endl << ++a << std::endl;
// Output: 0
// 1.
// "a" get incremented only by the prefix operator++
}
#包括
int main(){
int a=0;
std::couta++
递增a
并返回其值的副本,即1。这里没有什么意外。两种用法都在一个语句中。您假设某种从左到右的顺序不正确。@Jaa-c它在递增之前返回值的副本,即(应该是?)零。谢谢Christian Hackl,Jaa-c,如果在同一表达式中对同一对象使用两次++
,starkIt很容易遇到未定义的行为。即使行为定义良好,您仍然有可能混淆自己和代码的未来读者。帮个忙,拆分表达式:std::couthank you Santiago Varela:)没问题@Romeo.Upvote也请投票:)我投票了:),但bcoz我的声望不到15,得到的信息是它不会被展示
#include <iostream>
int main() {
int a = 0;
std::cout << a++ << std::endl
// Output: 0. ++ as posfix creates an incremented copy of "a" and AFTER the line gets executed.
std::cout << ++a << std::endl;
// Output: 1. ++ as prefix creates a copy of "a", an incremented copy BEFORE the line gets executed.
std::cout << a++ << std::endl << ++a << std::endl;
// Output: 0
// 1.
// "a" get incremented only by the prefix operator++
}