简体   繁体   English

将 arrays 与 for 循环一起使用

[英]Using arrays with for-loops

Questions:问题:

  1. why is the first "cin >>" having "score[0]" saved to it?为什么第一个“cin >>”保存了“score [0]”? Since the program is asking for 5 numbers, wouldn't it make sense to save the entered numbers into an array of 5 ("score[4]")?由于程序要求输入 5 个数字,将输入的数字保存到 5 的数组(“score[4]”)中是否有意义?

  2. I also don't understand the syntax of the second "cin >> score[i]."我也不明白第二个“cin >> score[i]”的语法。 I thought "cin>>" was coupled with "cout<<" when there was data input.当有数据输入时,我认为“cin>>”与“cout<<”相结合。

//Enter five scores. Show how much each differs from the highest score.
#include <iostream>
using namespace std;
    
int main()
{
    int i, score[5], max;
        
    cout<<"Enter 5 scores:\n";
    cin >> score[0];
    max = score[0];
        
    for (i = 1; i < 5; i++)
    {
        cin >> score[i];
        if (score[i] > max)
            max = score[i];
    }
        
    cout <<"Highest score: " <<max<<endl
         <<"The scores and their\n"
         <<"diff. from highest are:\n";
        
    for (i = 0; i < 5; i++)
        cout << score[i] << " off by "
             << (max - score[i]) << endl;
    
    return 0;        
}

cin is stdin . cin 是stdin This is a UNIX thing.这是一个 UNIX 的东西。 Basically it's the input to your program.基本上它是你程序的输入。 If you don't do anything else, it's your console or terminal session.如果您不做任何其他事情,那就是您的控制台或终端 session。

cout is stdout , another UNIX thing. cout 是stdout ,另一个 UNIX 东西。 It's output.它是 output。 Again, your console or terminal session if you don't do anything else with it.同样,如果您不使用它做任何其他事情,您的控制台或终端 session。 They aren't really coupled.它们并不是真正的耦合。 It's two separate things, one for input only, one for output only.这是两件不同的事情,一件仅用于输入,一件仅用于 output。

Now, let's look at your code:现在,让我们看看您的代码:

cin >> score[0];
max = score[0];
    
for (i = 1; i < 5; i++)
{
    cin >> score[i];
    if (score[i] > max)
        max = score[i];
}

This could be simplified.这可以简化。 You could get rid of the first two lines and change it to just this:您可以摆脱前两行并将其更改为:

for (i = 0; i < 5; i++)
{
    cin >> score[i];
    if (score[i] > max)
        max = score[i];
}

You'll just have to initialize max to a really negative value.您只需要将 max 初始化为一个真正的负值。

What cin >> does here is read a SINGLE value -- one score -- and then you stuff it into a single member of score . cin >>在这里所做的是读取一个 SINGLE 值 - 一个分数 - 然后将其填充到score的单个成员中。 So you can NOT do something like this:所以你不能做这样的事情:

cin >> score;

That just won't work.那是行不通的。 Well, you might be able to make it work with operator overloading, but that's an advanced topic, and I've never tried.好吧,您也许可以使其与运算符重载一起使用,但这是一个高级主题,我从未尝试过。 (I frankly never use the >> operator but find other ways to get input.) (坦率地说,我从不使用>>运算符,而是寻找其他方式来获取输入。)

Another thing: score[4] refers not to an array of size 5, but to the 5th item in the array, just like score[0] refers to the first item.还有一点: score[4]指的不是一个大小为 5 的数组,而是数组中的第 5 项,就像score[0]指的是第一项一样。 It only refers to the size in the initial definition:它仅指初始定义中的大小:

int score[5];

That's the only time the [5] is about the size.那是唯一一次[5]大约是大小。 Otherwise it's an index into the array, starting at 0. So you have score[0] ... score[4] .否则它是数组的索引,从 0 开始。所以你有score[0] ... score[4]

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

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