std::cin 在 cpp 中的工作原理

How std::cin works in cpp

我想知道std::cin在c++中是如何实现的。有没有可能有人写了一个伪代码就像 std::cin.

你不能在C++中做任何input/output而不使用一些库(或一些目标特定的汇编代码),因为您需要调用外部输出函数(或某些 system call,或者,如果您正在编写操作系统内核或其他一些独立程序,请执行一些 IO 机器代码指令)。

如果您正在编写一些以上的代码,operating system (this is generally the case), you might make some direct system calls. On Linux, they are listed in syscalls(2) (but in practice you'll use some system libc.so shared library). On Windows (which I don't know) the exact set of (kernel-level) system calls is harder to get, but you could use some system DLLs related to the WinAPI. You could read Operating Systems: Three Easy Pieces 以了解有关操作系统的更多信息。

很多 - 但不是全部 - C++ standard library implementations are free software, and often coded above some C standard library and/or above target-specific system calls. So you could study the source code of your C++ standard library (often released with the compiler, e.g. in GCC) and the source code of your C standard library (e.g. GNU libc or musl-libc).

因此,如果您在 Linux 上使用 GCC,请研究其源代码。

顺便说一句 cin >> 操作是一大组重载 std::istream::operator >> member functions and global 函数。