喜欢翻代码的同学肯定知道,cin >> s 的返回值是 istream 类型的对象的引用,当读取成功时,该对象的状态是 true,循环继续;当读取失败或遇到 EOF 时,该对象的状态变为 false,循环终止
所以 cin 在遇到输入结束(例如 EOF,End Of File)或读取失败时,会返回一个 false 值,从而使得 while (cin >> s) 循环终止
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 2e5 + 5; int __t = 1, n; void solve() { string s; while (cin >> s) ; cout << s.length() << endl; } int32_t main() { #ifdef ONLINE_JUDGE ios::sync_with_stdio(false); cin.tie(0); #endif // cin >> __t; while (__t--) solve(); return 0; }