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

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

      1. 字符串 c_str() 与 data()

        string c_str() vs. data()(字符串 c_str() 与 data())
          <tbody id='IeczG'></tbody>

            • <bdo id='IeczG'></bdo><ul id='IeczG'></ul>
              <tfoot id='IeczG'></tfoot>

              • <small id='IeczG'></small><noframes id='IeczG'>

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

                  <legend id='IeczG'><style id='IeczG'><dir id='IeczG'><q id='IeczG'></q></dir></style></legend>
                  本文介绍了字符串 c_str() 与 data()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我读过几个地方,c_str()data()(在 STL 和其他实现中)的区别在于 c_str()code> 总是空终止而 data() 不是.就我在实际实现中所见,它们要么执行相同操作,要么data() 调用 c_str().

                  I have read several places that the difference between c_str() and data() (in STL and other implementations) is that c_str() is always null terminated while data() is not. As far as I have seen in actual implementations, they either do the same or data() calls c_str().

                  我在这里错过了什么?在哪些场景下使用哪个更正确?

                  What am I missing here? Which one is more correct to use in which scenarios?

                  推荐答案

                  文档是正确的.使用 c_str() 如果你想要一个空终止字符串.

                  The documentation is correct. Use c_str() if you want a null terminated string.

                  如果实施者碰巧实施了data()c_str() 而言你不必担心,如果你不需要字符串以空终止,仍然使用 data() ,在某些实现中,它可能会比 c_str() 表现得更好.

                  If the implementers happend to implement data() in terms of c_str() you don't have to worry, still use data() if you don't need the string to be null terminated, in some implementation it may turn out to perform better than c_str().

                  字符串不一定必须由字符数据组成,它们可以由任何类型的元素组成.在这些情况下 data() 更有意义.c_str() 在我看来,只有当你的字符串元素是基于字符的时候才真正有用.

                  strings don't necessarily have to be composed of character data, they could be composed with elements of any type. In those cases data() is more meaningful. c_str() in my opinion is only really useful when the elements of your string are character based.

                  额外:从 C++11 开始,两个函数必须相同.即 data 现在需要以 null 结尾.根据cppreference:返回的数组以空值结尾,即, data() 和 c_str() 执行相同的功能."

                  Extra: In C++11 onwards, both functions are required to be the same. i.e. data is now required to be null-terminated. According to cppreference: "The returned array is null-terminated, that is, data() and c_str() perform the same function."

                  这篇关于字符串 c_str() 与 data()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与自定义类对象的向量一起使用?)
                  1. <tfoot id='9DCPn'></tfoot>

                      <legend id='9DCPn'><style id='9DCPn'><dir id='9DCPn'><q id='9DCPn'></q></dir></style></legend>

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

                          <small id='9DCPn'></small><noframes id='9DCPn'>

                          • <bdo id='9DCPn'></bdo><ul id='9DCPn'></ul>