在 C++ 中循环字符时出现意外结果
我正在使用《使用 C++ 进行编程原理和实践》一书来学习编程,其中一个练习是使用 while 循环遍历字符 az。
现在,我之前已经使用 C++ 和其他语言进行过编程,因此我决定尝试使用尽可能少的行(同时仍然使用 while 循环)。然而,这伴随着我的输出成本有点混乱。
代码:
#include <iostream>
int main(){
char ch = 'a';
while(ch <= 'z')
std::cout << ch << '\t' << (int) ch++ << std::endl;
return 0;
}
输出:
b 97
c 98
d 99
e 100
...
x 119
y 120
z 121
{ 122
现在我确实意识到这可以通过 for 循环来完成,我也这样做了(它有效)。但我仍然不明白这段代码有什么问题,这真的很烦人。
看起来好像我已经告诉它输出“ch+1”,因为它在应该打印“a”的地方打印出“b”。直到 ch 的整数值被放入输出流(后增量)之后,增量才会完成。即使我之前增加了它,至少打印的字符和它们的整数值应该对应。
关于为什么这段代码不起作用的任何想法?
I'm using the book "Programming Principles and Practice using C++" to learn programming and one of the exercises is looping through the characters a-z using a while-loop.
Now, I have programmed with C++ and other languages before, so I decided to try to use as few lines as possible (while still using a while-loop). However, this came with the cost of my output being kind of messed up.
Code:
#include <iostream>
int main(){
char ch = 'a';
while(ch <= 'z')
std::cout << ch << '\t' << (int) ch++ << std::endl;
return 0;
}
Output:
b 97
c 98
d 99
e 100
...
x 119
y 120
z 121
{ 122
Now I do realize that this could've been done with a for-loop instead, and I did that as well (it worked). But I still don't get what's wrong with this code, and it's really annoying me.
It appears as if I've told it to output "ch+1", since it prints out 'b' where it should print 'a'. The incrementing isn't done until after the integer value of ch has been put into the out-stream (post-increment). And even if I had incremented it earlier, at least the printed characters and their integer values should correspond.
Any ideas of why this code isn't working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
运算符<< 调用的顺序已明确指定,但其操作数的求值顺序却未明确指定。
ch
的增量可能发生在“第一次”输出ch
之前或之后,并且由于交错的读/写操作,仅仅运行该程序无论如何都是未定义的:相反,要明确:
您的编译器应该就您的代码发出警告。这并不表明您没有将诊断设置在有用的级别。
对于 GCC,请使用
-Wall -Wextra -std=c++98 -pedantic
(或对于 C++ 使用-Wall -Wextra -std=c++0x -pedantic
0x)。The order of the
operator<<
calls is well-specified, but the order in which their operands is evaluated is not.The increment of
ch
may happen before or after you outputch
"the first time", and merely running this program is Undefined anyway because of the interleaved read/write operations:Instead, be explicit:
Your compiler should have warned you about your code. That it didn't indicates that you do not have your diagnostics set at a useful level.
With GCC, use
-Wall -Wextra -std=c++98 -pedantic
(or-Wall -Wextra -std=c++0x -pedantic
for C++0x).简短的回答是,您有一些未定义的行为,因为您在同一表达式中通过单独的子表达式修改和使用变量
ch
的值。这里要做的正确的事情是使用最实用的循环来完成手头的任务:
Short answer is that you have a bit of Undefined Behavior because you're both modifying and using the value of the variable
ch
, via separate sub-expressions, in the same expression.The Right Thing(TM) to do here is to use the most practical loop for the task at hand:
首先评估代码中的 ch++。更具可读性和正确性的版本是:
The ch++ in your code gets evaluated first. A more readable and correct version would be:
当您在一个命令或表达式中使用一个变量两次时,一次使用 ++(或 --),一次不使用,则会出现未定义的行为。
而是使用:
When you use one variable twice in one command or expression, once with ++ (or --) and once without, you get undefined behavour.
Instead use: