我如何在 C++ 中使用 Cin 输入 1 行?

for*_*ner 0 c++

就像 if ab是变量 then

cin>>a>>b;
Run Code Online (Sandbox Code Playgroud)

像这样,我可以用 1 取多少个变量cin

for*_*818 7

对措辞进行一些挑剔:只有一个std::cin。它是一个类型的对象std::istream。它有一个operator>>让你一次读一件事。当操作符返回一个引用时,std::cin您可以根据需要链接任意数量的调用。

考虑到这两个正在做同样的事情:

 std::cin >> a;
 std::cin.operator>>(a);
Run Code Online (Sandbox Code Playgroud)

链接是通过

 std::cin >> a >> b;
 std::cin.operator>>(a).operator>>(b);
Run Code Online (Sandbox Code Playgroud)

因为每次调用都会operator>>返回对流的引用,所以在一个语句中可以读取的变量数量没有限制:

 int a,b,c,d,e,f;
 std::cin >> a >> b >> c >> d >> e >> f;
Run Code Online (Sandbox Code Playgroud)

虽然已经有 2 个变量,但您应该考虑它们是否属于相同的数据结构

 struct a_and_b {
     int a;
     int b;
 };
Run Code Online (Sandbox Code Playgroud)

然后你可以为 operator>>

std::istream& operator>>(std::istream& in,a_and_b& x) {
       in >> x.a;
       in >> x.b;
       return in;
};
Run Code Online (Sandbox Code Playgroud)

然后使用更具可读性的:

a_and_b x;
std::cin >> x;
Run Code Online (Sandbox Code Playgroud)