1. <tfoot id='iGf1G'></tfoot>

  2. <i id='iGf1G'><tr id='iGf1G'><dt id='iGf1G'><q id='iGf1G'><span id='iGf1G'><b id='iGf1G'><form id='iGf1G'><ins id='iGf1G'></ins><ul id='iGf1G'></ul><sub id='iGf1G'></sub></form><legend id='iGf1G'></legend><bdo id='iGf1G'><pre id='iGf1G'><center id='iGf1G'></center></pre></bdo></b><th id='iGf1G'></th></span></q></dt></tr></i><div id='iGf1G'><tfoot id='iGf1G'></tfoot><dl id='iGf1G'><fieldset id='iGf1G'></fieldset></dl></div>
  3. <small id='iGf1G'></small><noframes id='iGf1G'>

      • <bdo id='iGf1G'></bdo><ul id='iGf1G'></ul>

      <legend id='iGf1G'><style id='iGf1G'><dir id='iGf1G'><q id='iGf1G'></q></dir></style></legend>

      为什么 std::fstream 类不采用 std::string?

      时间:2023-08-25
      1. <tfoot id='ZBrEK'></tfoot>
        <legend id='ZBrEK'><style id='ZBrEK'><dir id='ZBrEK'><q id='ZBrEK'></q></dir></style></legend>

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

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

              <tbody id='ZBrEK'></tbody>
              • 本文介绍了为什么 std::fstream 类不采用 std::string?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                这不是一个设计问题,真的,虽然看起来像.(好吧,这是一个设计问题).我想知道的是为什么 C++ std::fstream 类在它们的构造函数或 open 方法中不采用 std::string .每个人都喜欢代码示例,所以:

                This isn't a design question, really, though it may seem like it. (Well, okay, it's kind of a design question). What I'm wondering is why the C++ std::fstream classes don't take a std::string in their constructor or open methods. Everyone loves code examples so:

                #include <iostream>
                #include <fstream>
                #include <string>
                
                int main()
                {
                    std::string filename = "testfile";      
                    std::ifstream fin;
                
                    fin.open(filename.c_str()); // Works just fine.
                    fin.close();
                
                    //fin.open(filename); // Error: no such method.
                    //fin.close();
                }
                

                这让我在处理文件时一直都在使用.C++ 库肯定会尽可能使用 std::string 吗?

                This gets me all the time when working with files. Surely the C++ library would use std::string wherever possible?

                推荐答案

                通过采用 C 字符串,C++03 std::fstream 类减少了对 std::string 类的依赖.然而,在 C++11 中,std::fstream 类允许为它的构造函数参数传递一个 std::string.

                By taking a C string the C++03 std::fstream class reduced dependency on the std::string class. In C++11, however, the std::fstream class does allow passing a std::string for its constructor parameter.

                现在,您可能想知道为什么没有从 std:string 到 C 字符串的透明转换,因此需要 C 字符串的类仍然可以采用 std::string 就像一个需要 std::string 的类可以接受一个 C 字符串.

                Now, you may wonder why isn't there a transparent conversion from a std:string to a C string, so a class that expects a C string could still take a std::string just like a class that expects a std::string can take a C string.

                原因是这会导致转换周期,进而可能导致问题.例如,假设 std::string 可转换为 C 字符串,以便您可以将 std::strings 与 fstreams 一起使用.还假设 C 字符串可转换为 std::strings,就像当前标准中的状态一样.现在,请考虑以下事项:

                The reason is that this would cause a conversion cycle, which in turn may lead to problems. For example, suppose std::string would be convertible to a C string so that you could use std::strings with fstreams. Suppose also that C string are convertible to std::strings as is the state in the current standard. Now, consider the following:

                void f(std::string str1, std::string str2);
                void f(char* cstr1, char* cstr2);
                
                void g()
                {
                    char* cstr = "abc";
                    std::string str = "def";
                    f(cstr, str);  // ERROR:  ambiguous
                }
                

                因为您可以在 std::string 和 C 字符串之间转换任何一种方式,所以对 f() 的调用可以解析为两个 f() 替代方案,因此是模棱两可的.解决方案是通过明确一个转换方向来打破转换循环,这就是 STL 选择用 c_str() 做的事情.

                Because you can convert either way between a std::string and a C string the call to f() could resolve to either of the two f() alternatives, and is thus ambiguous. The solution is to break the conversion cycle by making one conversion direction explicit, which is what the STL chose to do with c_str().

                这篇关于为什么 std::fstream 类不采用 std::string?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:将文件内容读入 C++ 中的字符串 下一篇:如何表示循环中不再有字符串 ss 的输入 while (cin &gt;&gt; ss)

                相关文章

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

                    <small id='2esh0'></small><noframes id='2esh0'>

                      <bdo id='2esh0'></bdo><ul id='2esh0'></ul>

                    <tfoot id='2esh0'></tfoot>