C++ 是什么意思? iostreams 必须提供与 C stdio 库相比的功能吗?

发布于 2024-10-03 04:22:52 字数 325 浏览 4 评论 0原文

可能的重复:
您在 C++ 代码中使用哪个 I/O 库?

我在对另一个问题的评论中提出了这个问题,并被要求将其作为一个正确的问题。

为什么我想使用 iostream 而不是 stdio?更具体地说,与 C 等效项相比,std::getline 必须提供什么?

拜托,不要语言攻击。

Possible Duplicate:
Which I/O library do you use in your C++ code?

I asked this question in a comment to another question, and I was asked to make it a proper question instead.

Why do I want to use iostream instead of stdio? More specifically, what do std::getline have to offer over the C equivalent?

Please, no language bashing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

全部不再 2024-10-10 04:22:52

有几个优点,主要是使用 <<>> 运算符。获取一行并没有什么不同,尽管能够将其读入 std::string 是一个相当大的优势。

C++ I/O 具有类型安全性。您不会将参数列表编写为带引号的字符串,然后再次编写为变量等。您编写一次要打印的内容,然后 C++ 计算出有多少个参数以及它们的类型。当类型不匹配时,CI/O 可能会得到错误的 I/O,甚至尝试访问受保护的内存。

C++ I/O 很容易扩展。一旦您获得了要复制的示例,您就可以轻松编写 operator<<()operator>>()printf() 及其友元无法扩展。您有一个固定的格式类型列表。

C++ I/O 虽然乍一看相当简单,但具有许多程序员可访问的结构,因此优秀的 C++ 程序员可以对其进行修改以覆盖 CI/O 无法覆盖的情况。 (不要过度使用这个。)

There are several advantages, mostly with the << and >> operators. Getting a line isn't all that different, although being able to read it into a std::string is a considerable advantage.

C++ I/O has type safety. You don't write your parameter list as a quoted string, and then again as variables and such. You write what you're going to print once, and C++ figures out how many parameters and what type they are. When you have type mismatches, C I/O might get the I/O wrong, or even try to access protected memory.

C++ I/O is easily to extend. You can write operator<<() and operator>>() easily, once you've got a sample to copy. printf() and friends cannot be extended. You have a fixed list of format types.

C++ I/O, while it looks fairly simple at first, has a lot of programmer-accessible structure, and therefore a good C++ programmer can modify it to cover cases that C I/O can't. (Don't overuse this.)

聊慰 2024-10-10 04:22:52

最大的收获是类型安全。 C 中的格式字符串没有类型(与 OCaml 或 boost::format 不同),因此您可能会意外地用它们做一些非常讨厌的事情。

The biggest gain is type safety. Format strings in C have no type (unlike in say, OCaml or boost::format) and so you can do some pretty nasty things with them by accident.

小帐篷 2024-10-10 04:22:52
  1. 您获得可以以任何方式实现的抽象输入/输出/可查找流。

    您可以从文件、内存、字符串甚至自定义过滤器或 zlib 压缩器写入/读取流!

    一些 C 库提供了设置读/写处理程序的选项(BSD 和 Linux),但仍然没有
    与 std::streambuf 具有相同的功能,但它们不是标准的。

  2. 您可以使用流特定区域设置,允许您根据要流式传输的任何区域设置格式化数据
    线程特定的方式。

  3. 类型安全。
  4. 写入和读取流通用对象(复杂变量、XML 对象等)

更多?

  1. You gain abstract input/output/seekable stream that can be implemented in any way.

    You write to /read from stream that may be file, memory, string or even custom filter or zlib compressor!

    Some C libraries provide an option for setting read/write handlers (BSD and Linux) but still do not have
    same power as std::streambuf and these are not standard.

  2. You may use stream specific locale allowing you to format data according to any locale to stream in
    thread specific way.

  3. Type safety.
  4. Writing to and reading from stream generic objects (complex variables, XML object etc.)

More?

油饼 2024-10-10 04:22:52

其一,如果您使用 iostream,那么您可以使用 std::string,而不是使用 char 数组,这意味着您需要担心的内存管理要少得多。

For one, if you use iostream then you get to work with std::string, rather then with char arrays, meaning that you have a lot less memory management to worry about.

中二柚 2024-10-10 04:22:52

好吧,如果您使用 C++,您可能想使用 OOP,对吧?我猜 cstdio(又名 stdio.h)只是为了与 C 兼容。

Well, if you use C++ you might want to use OOP, right? I guess cstdio (aka stdio.h) is there just for compatibility with C.

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