将 iostream 与 stdio 同步

发布于 2024-08-08 03:41:11 字数 316 浏览 1 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴骨ら 2024-08-15 03:41:12

这是 std::ios_base::sync_with_stdio() 的声明:

static boolsync_with_stdio( boolsync = true );

它设置每次输入/输出操作后是否将标准 C++ 流同步到标准 C 流。

标准 C++ 流如下:std::cinstd::coutstd::cerrstd:: clogstd::wcinstd::wcoutstd::wcerrstd::wclog< /代码>。

标准 C 流如下:stdinstdoutstderr

实际上,这意味着同步的 C++ 流是无缓冲的,并且 C++ 流上的每个 I/O 操作都会立即应用于相应的 C 流的缓冲区。这使得自由混合 C++ 和 CI/O 成为可能。

此外,同步的 C++ 流保证是线程安全的(从多个线程输出的单个字符可能会交错,但不会发生数据竞争)。

如果关闭同步,则允许 C++ 标准流独立缓冲其 I/O,这在某些情况下可能会快得多。

默认情况下,所有八个标准 C++ 流都与其各自的 C 流同步。

如果在标准流上发生 I/O 之后调用此函数,则行为是实现定义的:实现范围从无效到破坏读缓冲区。


示例:

#include <iostream>
#include <cstdio>

int main()
{
    std::ios::sync_with_stdio(false);
    std::cout << "a\n";
    std::printf("b\n");
    std::cout << "c\n";
}

输出:

b
a
c

来源:cppreference

This is the declaration of std::ios_base::sync_with_stdio():

static bool sync_with_stdio( bool sync = true );

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 and std::wclog.

The standard C streams are the following: stdin, stdout, and stderr.

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:

#include <iostream>
#include <cstdio>

int main()
{
    std::ios::sync_with_stdio(false);
    std::cout << "a\n";
    std::printf("b\n");
    std::cout << "c\n";
}

Output:

b
a
c

Source: cppreference

掩耳倾听 2024-08-15 03:41:11

默认情况下,流是同步的,保证按标准工作,您无需执行任何操作。 sync_with_stdio 仅用于在您需要时禁用同步。

从您提到的文章中:

对于预定义的流,它是安全的
混合 stdio 和 iostream。为了
例如,您可以安全地使用 stdin 和
cin 在同一程序中; C++
标准保证它会起作用
就像你天真的期望的那样
至。

唯一的缺点是潜在的性能影响(我想这就是它可以被禁用的原因)。

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 :

For the predefined streams, it's safe
to mix stdio and iostreams. For
example, you can safely use stdin and
cin in the same program; the C++
Standard guarantees that it will work
the way you would naively expect it
to.

The only drawback is a potential performance hit (I guess that's why it can be disabled).

情魔剑神 2024-08-15 03:41:11

正如 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文