<i id='mGLHa'><tr id='mGLHa'><dt id='mGLHa'><q id='mGLHa'><span id='mGLHa'><b id='mGLHa'><form id='mGLHa'><ins id='mGLHa'></ins><ul id='mGLHa'></ul><sub id='mGLHa'></sub></form><legend id='mGLHa'></legend><bdo id='mGLHa'><pre id='mGLHa'><center id='mGLHa'></center></pre></bdo></b><th id='mGLHa'></th></span></q></dt></tr></i><div id='mGLHa'><tfoot id='mGLHa'></tfoot><dl id='mGLHa'><fieldset id='mGLHa'></fieldset></dl></div>

      <bdo id='mGLHa'></bdo><ul id='mGLHa'></ul>
  1. <legend id='mGLHa'><style id='mGLHa'><dir id='mGLHa'><q id='mGLHa'></q></dir></style></legend><tfoot id='mGLHa'></tfoot>

      <small id='mGLHa'></small><noframes id='mGLHa'>

    1. 为什么 (i|o)fstream 为文件名采用 const char* 参数?

      Why does (i|o)fstream take a const char* parameter for a file name?(为什么 (i|o)fstream 为文件名采用 const char* 参数?)
      <i id='7L3j6'><tr id='7L3j6'><dt id='7L3j6'><q id='7L3j6'><span id='7L3j6'><b id='7L3j6'><form id='7L3j6'><ins id='7L3j6'></ins><ul id='7L3j6'></ul><sub id='7L3j6'></sub></form><legend id='7L3j6'></legend><bdo id='7L3j6'><pre id='7L3j6'><center id='7L3j6'></center></pre></bdo></b><th id='7L3j6'></th></span></q></dt></tr></i><div id='7L3j6'><tfoot id='7L3j6'></tfoot><dl id='7L3j6'><fieldset id='7L3j6'></fieldset></dl></div>
      <legend id='7L3j6'><style id='7L3j6'><dir id='7L3j6'><q id='7L3j6'></q></dir></style></legend>

    2. <small id='7L3j6'></small><noframes id='7L3j6'>

        <tbody id='7L3j6'></tbody>
          <tfoot id='7L3j6'></tfoot>
            • <bdo id='7L3j6'></bdo><ul id='7L3j6'></ul>
                本文介绍了为什么 (i|o)fstream 为文件名采用 const char* 参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                为什么std::(i|o)fstream类的构造函数和open方法以const char*<的形式将文件名作为参数/code> 而不是 std::string?似乎 STL 的创建者希望使用他们编写的内容,而不是使用他们编写的类来替换的类型.

                Why does the constructor and open method of the std::(i|o)fstream classes take the name of a file as a parameter in the form of a const char* instead of an std::string? It seems like the creators of the STL would want to use what they had written instead of using the type they wrote a class to replace.

                推荐答案

                Class std::string 实现了运行时大小可调整的字符串"的概念.这是应该使用这个类的时候——当你需要一个字符串,它的大小只在运行时知道,并且在运行时也可以调整大小.在您不需要这些功能的情况下,使用 std::string 是一种矫枉过正.显然,该库的作者并不认为他们需要一个运行时可调整大小的字符串来表示文件名,所以他们选择了一个简约的解决方案:他们使用了一个 C 字符串,其中一个 C 字符串就足够了.这实际上是设计库接口的一个很好的原则:永远不要需要你并不真正需要的东西.

                Class std::string implements the concept of "run-time-sized resizable string". This is when this class should be used - when you need a string whose size is only known at run-time and which is run-time resizable as well. In situations when you don't need these features using std::string is an overkill. Apparently, the authors of the library didn't think that they needed a run-time resizable string to represent a file name, so they opted for a minimalistic solution: they used a C-string where a C-string was sufficient. This is actually a very good principle for designing library interfaces: never require something that you don't really need.

                确实,现在我们经常看到有人鼓励 C++ 程序员在需要字符串时使用 std::string.他们经常声称经典的 C 字符串应该保留给 C 代码.在一般情况下,这是一种虚假的哲学.无偿使用像 std::string 这样相对较重的对象在 Java 等语言中更合适,但在 C++ 中通常是不可接受的.

                It is true that these days we often see people who encourage C++ programmers to use std::string whenever they need a string, any string. They often claim that classic C strings should be reserved to C code. In general case this is a bogus philosophy. Gratuitous use of comparatively heavy objects like std::string is more appropriate in languages like Java, but is normally unacceptable in C++.

                是的,在某些 C++ 应用程序中始终使用 std::string 是可能的(可以用 C++ 编写 Java 程序"),但在这样的情况下作为 C++ 标准库的通用低级库迫使用户在没有充分理由(即强加不必要的要求)的情况下使用 std::string 看起来不太好.

                Yes, it is possible to get away with using std::string all the time in some C++ applications ("it is possible to write a Java program in C++"), but in such a generic low-level library as C++ standard library forcing the user to use std::string without a good reason (i.e. imposing unnecessary requirements) would not look good.

                这篇关于为什么 (i|o)fstream 为文件名采用 const char* 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                Adding types to the std namespace(将类型添加到 std 命名空间)
                Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)

                <small id='G8dhW'></small><noframes id='G8dhW'>

                  <tbody id='G8dhW'></tbody>

                  <tfoot id='G8dhW'></tfoot>
                    <legend id='G8dhW'><style id='G8dhW'><dir id='G8dhW'><q id='G8dhW'></q></dir></style></legend>
                  1. <i id='G8dhW'><tr id='G8dhW'><dt id='G8dhW'><q id='G8dhW'><span id='G8dhW'><b id='G8dhW'><form id='G8dhW'><ins id='G8dhW'></ins><ul id='G8dhW'></ul><sub id='G8dhW'></sub></form><legend id='G8dhW'></legend><bdo id='G8dhW'><pre id='G8dhW'><center id='G8dhW'></center></pre></bdo></b><th id='G8dhW'></th></span></q></dt></tr></i><div id='G8dhW'><tfoot id='G8dhW'></tfoot><dl id='G8dhW'><fieldset id='G8dhW'></fieldset></dl></div>

                        <bdo id='G8dhW'></bdo><ul id='G8dhW'></ul>