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

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

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

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

        stringstream、string 和 char* 转换混淆

        时间:2023-09-16
      2. <small id='vNITc'></small><noframes id='vNITc'>

      3. <tfoot id='vNITc'></tfoot>

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

                <tbody id='vNITc'></tbody>

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

                  本文介绍了stringstream、string 和 char* 转换混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我的问题可以归结为,从 stringstream.str().c_str() 返回的字符串在内存中的位置,为什么不能分配给 const char*?

                  My question can be boiled down to, where does the string returned from stringstream.str().c_str() live in memory, and why can't it be assigned to a const char*?

                  这个代码示例比我能解释的更好

                  This code example will explain it better than I can

                  #include <string>
                  #include <sstream>
                  #include <iostream>
                  
                  using namespace std;
                  
                  int main()
                  {
                      stringstream ss("this is a string
                  ");
                  
                      string str(ss.str());
                  
                      const char* cstr1 = str.c_str();
                  
                      const char* cstr2 = ss.str().c_str();
                  
                      cout << cstr1   // Prints correctly
                          << cstr2;   // ERROR, prints out garbage
                  
                      system("PAUSE");
                  
                      return 0;
                  }
                  

                  假设 stringstream.str().c_str() 可以分配给 const char* 导致了一个错误,我花了一段时间才找到.

                  The assumption that stringstream.str().c_str() could be assigned to a const char* led to a bug that took me a while to track down.

                  对于加分,谁能解释为什么用

                  For bonus points, can anyone explain why replacing the cout statement with

                  cout << cstr            // Prints correctly
                      << ss.str().c_str() // Prints correctly
                      << cstr2;           // Prints correctly (???)
                  

                  正确打印字符串?

                  我正在 Visual Studio 2008 中编译.

                  I'm compiling in Visual Studio 2008.

                  推荐答案

                  stringstream.str() 返回在完整表达式结束时销毁的临时字符串对象.如果你从中得到一个指向 C 字符串的指针 (stringstream.str().c_str()),它将指向一个在语句结束处被删除的字符串.这就是为什么您的代码打印垃圾的原因.

                  stringstream.str() returns a temporary string object that's destroyed at the end of the full expression. If you get a pointer to a C string from that (stringstream.str().c_str()), it will point to a string which is deleted where the statement ends. That's why your code prints garbage.

                  您可以将该临时字符串对象复制到某个其他字符串对象并从该对象中获取 C 字符串:

                  You could copy that temporary string object to some other string object and take the C string from that one:

                  const std::string tmp = stringstream.str();
                  const char* cstr = tmp.c_str();
                  

                  请注意,我创建了临时字符串 const,因为对它的任何更改都可能导致它重新分配,从而使 cstr 无效.因此,完全不存储对 str() 的调用结果并仅在完整表达式结束之前使用 cstr 更安全:

                  Note that I made the temporary string const, because any changes to it might cause it to re-allocate and thus render cstr invalid. It is therefor safer to not to store the result of the call to str() at all and use cstr only until the end of the full expression:

                  use_c_str( stringstream.str().c_str() );
                  

                  当然,后者可能并不容易,复制可能太昂贵了.您可以做的是将临时对象绑定到 const 引用.这会将其生命周期延长到引用的生命周期:

                  Of course, the latter might not be easy and copying might be too expensive. What you can do instead is to bind the temporary to a const reference. This will extend its lifetime to the lifetime of the reference:

                  {
                    const std::string& tmp = stringstream.str();   
                    const char* cstr = tmp.c_str();
                  }
                  

                  IMO 这是最好的解决方案.不幸的是,它不是很出名.

                  IMO that's the best solution. Unfortunately it's not very well known.

                  这篇关于stringstream、string 和 char* 转换混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:std::launder 的目的是什么? 下一篇:什么时候在 C 中使用位域?

                  相关文章

                  最新文章

                • <tfoot id='QcFpd'></tfoot>

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

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