简体   繁体   English

需要非常简单的C ++ for循环帮助

[英]Need assistance with very simple C++ for loop

for( int i = 0; i <= 10; i++ )
{
    cout << setw(2) << i << setw(10) << ( i <= 5 ? cout <<" = Chipotle" : cout << " = McDonalds" ) << endl;
}

So, I expected the output to be: 所以,我预计输出为:

 0 = Chipotle
 1 = Chipotle
 2 = Chipotle
 3 = Chipotle
 4 = Chipotle
 5 = Chipotle
 6 = McDonalds
 7 = McDonalds
 8 = McDonalds
 9 = McDonalds
10 = McDonalds

(Don't worry about the setw formatting) (不要担心setw格式化)

Instead, my IDE gave me: 相反,我的IDE给了我:

 = Chipotle 0  0x602208                                                                                                             
 = Chipotle 1  0x602208                                                                                                             
 = Chipotle 2  0x602208                                                                                                             
 = Chipotle 3  0x602208                                                                                                             
 = Chipotle 4  0x602208                                                                                                             
 = Chipotle 5  0x602208                                                                                                             
 = McDonalds 6  0x602208                                                                                                            
 = McDonalds 7  0x602208                                                                                                            
 = McDonalds 8  0x602208                                                                                                            
 = McDonalds 9  0x602208                                                                                                            
 = McDonalds10  0x602208

Where did I went wrong? 我哪里出错了?

你显示cout本身,删除三元运算符中的cout

cout << setw(2) << i << setw(10) << ( i <= 5 ? " = Chipotle" : " = McDonalds" ) << endl

You are trying to cout a cout. 你想尝试一个cout。

( i <= 5 ? cout <<" = Chipotle" : cout << " = McDonalds" )

becomes either 成为

cout <<" = Chipotle" 
//or 
cout << " = McDonalds"

So you are trying to do 所以你要努力做到

cout << setw(2) << i << setw(10) << cout <<" = Chipotle" 
//or
cout << setw(2) << i << setw(10) << cout <<" = McDonalds" 

Which is incorrect. 这是不正确的。 You need to change you code to 您需要将代码更改为

( i <= 5 ? " = Chipotle" : " = McDonalds" )

Which will expand to 哪个将扩展到

cout << setw(2) << i << setw(10) << " = Chipotle" 
//or
cout << setw(2) << i << setw(10) << " = McDonalds" 

When i <= 5 is true, this i <= 5 ,这个

cout << i << ( i <= 5 ? cout <<" = Chipotle" : cout << " = McDonalds" ) << endl

evaluates like this: 评估如下:

cout << i << (cout << " = Chipotle") << endl

I omitted the setw manipulators here to make the code easier to read, which would affect spacing in the result but makes no difference otherwise. 我在这里省略了setw操纵器以使代码更容易阅读,这会影响结果中的间距但是没有区别。 The result is the same as this sequence of operations: 结果与此操作序列相同:

cout << " = Chipotle"; // the thing in `()` gets evaluated first
cout << i;
cout << cout; // because (cout << " = Chipotle") evaluates to cout
cout << endl;

which is exactly what you see for the first few lines. 这正是你在前几行看到的。 cout itself is printed out as 0x602208 . cout本身打印为0x602208 After that you get McDonalds instead of Chipotle . 之后你会得到McDonalds而不是Chipotle

If you just write cout once, at the left end of your output expression, you will get the output you want. 如果只编写一次cout ,在输出表达式的左端,您将获得所需的输出。

You do not have to write cout again inside the if condition you have written using the ?: operators. 您不必在使用?:运算符编写的if条件内再次编写cout。 Just place the string what you want to print without the cout 只需将字符串放在您想要打印的内容中,而无需使用cout

This is more precise and readable approach. 这是更精确和可读的方法。

for( int i = 0; i <= 10; i++ )
{
    cout << setw(2) << i << setw(10) ;
    if(i<= 5)
    {
        cout <<" = Chipotle"<<endl;
    }
    else
    {
        cout << " = McDonalds" << endl;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM