将 iostream 与 stdio 同步
我正在尝试将 iostream 添加到遗留代码中,因此想要同步这两个库。 根据这篇文章 ,我应该使用 std::ios_base::sync_with_stdio。
现在,我想知道它在实践中是如何使用的(请举例),我应该注意的副作用。
谢谢
I am trying to add iostream to the legacy code and thus want to sync those two libraries.
According to this article, I should use std::ios_base::sync_with_stdio.
Now, I wonder how it is used in practice (examples please), side-effects I should be aware of.
Thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 std::ios_base::sync_with_stdio() 的声明:
它设置每次输入/输出操作后是否将标准 C++ 流同步到标准 C 流。
标准 C++ 流如下:
std::cin
、std::cout
、std::cerr
、std:: clog
、std::wcin
、std::wcout
、std::wcerr
和std::wclog< /代码>。
标准 C 流如下:
stdin
、stdout
和stderr
。实际上,这意味着同步的 C++ 流是无缓冲的,并且 C++ 流上的每个 I/O 操作都会立即应用于相应的 C 流的缓冲区。这使得自由混合 C++ 和 CI/O 成为可能。
此外,同步的 C++ 流保证是线程安全的(从多个线程输出的单个字符可能会交错,但不会发生数据竞争)。
如果关闭同步,则允许 C++ 标准流独立缓冲其 I/O,这在某些情况下可能会快得多。
默认情况下,所有八个标准 C++ 流都与其各自的 C 流同步。
如果在标准流上发生 I/O 之后调用此函数,则行为是实现定义的:实现范围从无效到破坏读缓冲区。
示例:
输出:
来源:cppreference
This is the declaration of
std::ios_base::sync_with_stdio()
:It sets whether the standard C++ streams are synchronized to the standard C streams after each input/output operation.
The standard C++ streams are the following:
std::cin
,std::cout
,std::cerr
,std::clog
,std::wcin
,std::wcout
,std::wcerr
andstd::wclog
.The standard C streams are the following:
stdin
,stdout
, andstderr
.In practice, this means that the synchronized C++ streams are unbuffered, and each I/O operation on a C++ stream is immediately applied to the corresponding C stream's buffer. This makes it possible to freely mix C++ and C I/O.
Also, synchronized C++ streams are guaranteed to be thread-safe (individual characters output from multiple threads may interleave, but no data races occur)
If the synchronization is turned off, the C++ standard streams are allowed to buffer their I/O independently, which may be considerably faster in some cases.
By default, all eight standard C++ streams are synchronized with their respective C streams.
If this function is called after I/O has occurred on the standard stream, the behavior is implementation-defined: implementations range from no effect to destroying the read buffer.
Example:
Output:
Source: cppreference
默认情况下,流是同步的,保证按标准工作,您无需执行任何操作。
sync_with_stdio
仅用于在您需要时禁用同步。从您提到的文章中:
唯一的缺点是潜在的性能影响(我想这就是它可以被禁用的原因)。
By default the streams are synchronized, it's guaranteed to work by the standard, you don't have to do anything.
sync_with_stdio
is only here to disable synchronisation if you want to.From the article you mentioned :
The only drawback is a potential performance hit (I guess that's why it can be disabled).
正如 TheSamFrom1984 所说,同步是默认设置,因此应该不是问题。然而,只有当两个库操作同一个流时,同步才相关。这通常在分别使用 cin/cout/cerr 和 stdin/stdout/stderr 时发生。然而,除了重用遗留代码之外,我认为没有什么理由需要同时使用两者。
当我第一次开始使用 C++ 时,我发现自己这样做是因为我经常知道如何使用 stdio 执行某些操作,但不知道如何使用 iostream 执行此操作,但更好的方法是弄清楚如何在一个或多个中执行操作其他,但不是两者。
As TheSamFrom1984 says, synced is the default so it should not be a problem. However synchronisation is only relevant when the same stream is being operated on by both libraries. This typically occurs when using cin/cout/cerr and stdin/stdout/stderr respectively. However I can see few reasons for needing to use both simultaneously except when reusing legacy code.
When I first started using C++ I found myself doing this because often I knew how to do something using stdio, but did not know how to do it with iostream, but a better approach would be to figure out how to do in in one or the other, but not both.