gpt4 book ai didi

c++ - cin 如何拆分整数序列?

转载 作者:行者123 更新时间:2023-11-30 03:44:22 24 4
gpt4 key购买 nike

cin 如何拆分整数序列?下面给出了一个例子:

#include <iostream>
int main() {
int sum = 0, value = 0;
while (std::cin >> value) {
sum += value;
}
std::cout << "Sum is: " << sum << std::endl;
return 0;
}

所以如果输入是:3 10 30 2 5输出将是:50

但是这是怎么发生的呢?这些值是否被视为某种数组/列表,或者单个变量的值是否由于 while 循环而被更改?

这里是初学者。

最佳答案

这是来自网站http://www.cplusplus.com/doc/tutorial/basic_io/ 的摘录:

Extractions on cin can also be chained to request more than one datum in a single statement:

cin >> a >> b;

This is equivalent to:

1 2

cin >> a; cin >> b;

In both cases, the user is expected to introduce two values, one for variable a, and another for variable b. Any kind of space is used to separate two consecutive input operations; this may either be a space, a tab, or a new-line character.

关于c++ - cin 如何拆分整数序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35533262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com