• <legend id='Hr9ik'><style id='Hr9ik'><dir id='Hr9ik'><q id='Hr9ik'></q></dir></style></legend>

    <tfoot id='Hr9ik'></tfoot>

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

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

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

        你如何构造一个带有嵌入空值的 std::string ?

        时间:2023-12-02
          <bdo id='iE7xW'></bdo><ul id='iE7xW'></ul>

            • <legend id='iE7xW'><style id='iE7xW'><dir id='iE7xW'><q id='iE7xW'></q></dir></style></legend>

              1. <small id='iE7xW'></small><noframes id='iE7xW'>

                    <tbody id='iE7xW'></tbody>

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

                  本文介绍了你如何构造一个带有嵌入空值的 std::string ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我想用如下一行构造一个 std::string:

                  If I want to construct a std::string with a line like:

                  std::string my_string("ab");
                  

                  如果我想在结果字符串中包含三个字符(a、null、b),我只得到一个.正确的语法是什么?

                  Where i want to have three characters in the resulting string (a, null, b), I only get one. What is the proper syntax?

                  推荐答案

                  自 C++14

                  我们已经能够创建文字std::string

                  #include <iostream>
                  #include <string>
                  
                  int main()
                  {
                      using namespace std::string_literals;
                  
                      std::string s = "pl--op"s;    // <- Notice the "s" at the end
                                                      // This is a std::string literal not
                                                      // a C-String literal.
                      std::cout << s << "
                  ";
                  }
                  

                  在 C++14 之前

                  问题是采用 const char*std::string 构造函数假定输入是 C 字符串.C 字符串被 终止,因此当它到达 字符时解析停止.

                  Before C++14

                  The problem is the std::string constructor that takes a const char* assumes the input is a C-string. C-strings are terminated and thus parsing stops when it reaches the character.

                  为了弥补这一点,您需要使用从 char 数组(不是 C 字符串)构建字符串的构造函数.这需要两个参数 - 一个指向数组的指针和一个长度:

                  To compensate for this, you need to use the constructor that builds the string from a char array (not a C-String). This takes two parameters - a pointer to the array and a length:

                  std::string   x("pqrs");   // Two characters because input assumed to be C-String
                  std::string   x("pqrs",5); // 5 Characters as the input is now a char array with 5 characters.
                  

                  注意:C++ std::stringNOT 终止的(如其他帖子中所建议的那样).但是,您可以使用 c_str() 方法提取指向包含 C 字符串的内部缓冲区的指针.

                  Note: C++ std::string is NOT -terminated (as suggested in other posts). However, you can extract a pointer to an internal buffer that contains a C-String with the method c_str().

                  另请查看 Doug T 的回答 下面关于使用 vector.

                  Also check out Doug T's answer below about using a vector<char>.

                  另请查看 RiaD 以获取 C++14 解决方案.

                  Also check out RiaD for a C++14 solution.

                  这篇关于你如何构造一个带有嵌入空值的 std::string ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:std::string 是否有空终止符? 下一篇:使用 nullptr 有什么好处?

                  相关文章

                    <tfoot id='VJMuP'></tfoot>
                  1. <small id='VJMuP'></small><noframes id='VJMuP'>

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

                    1. <legend id='VJMuP'><style id='VJMuP'><dir id='VJMuP'><q id='VJMuP'></q></dir></style></legend>