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

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

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

      <tfoot id='UqFli'></tfoot>

        我什么时候会通过 const&amp;std::string 而不是 std::string_view?

        时间:2024-08-14

          <tfoot id='p7Xd3'></tfoot>
            <bdo id='p7Xd3'></bdo><ul id='p7Xd3'></ul>

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

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

                <tbody id='p7Xd3'></tbody>
                1. 本文介绍了我什么时候会通过 const&amp;std::string 而不是 std::string_view?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我了解使用 std::string_view 的动机;
                  它可以帮助避免函数参数中不必要的分配.

                  I understand the motivation for using std::string_view;
                  it can help avoid unecessary allocations in function arguments.

                  例如:
                  以下程序将从字符串文字创建 std::string.
                  这会导致不希望的动态分配,因为我们只对观察字符感兴趣.

                  For example:
                  The following program will create a std::string from a string literal.
                  This causes an undesired dynamic allocation, as we are only interested observing the characters.

                  #include <iostream>
                  
                  void* operator new(std::size_t n)
                  {
                      std::cout << "[allocating " << n << " bytes]
                  ";
                      return malloc(n);
                  }
                  
                  void observe_string(std::string const& str){}
                  
                  int main(){
                    observe_string("hello world"); //prints [allocating 36 bytes]
                  }
                  

                  使用 string_view 可以解决问题:

                  Using string_view will solve the problem:

                  #include <iostream>
                  #include <experimental/string_view>
                  
                  void* operator new(std::size_t n)
                  {
                      std::cout << "[allocating " << n << " bytes]
                  ";
                      return malloc(n);
                  }
                  
                  void observe_string(std::experimental::string_view const& str){
                  }
                  
                  int main(){
                    observe_string("hello world"); //prints nothing
                  }
                  

                  <小时>

                  这给我留下了一个问题.
                  我什么时候会通过 const& 选择 std::string而不是 string_view 函数参数?

                  std::string_view的接口,看起来好像可以替换const&传递的std::string的所有实例..有什么反例吗?std::string_view 是否意味着替换 std::string const& 用于参数传递?

                  Looking at the interface of std::string_view, it looks as though I could replace all instances of std::string that are passed by const&. Are there any counter examples to this? Is std::string_view meant to replace std::string const& for parameter passing?

                  推荐答案

                  我什么时候会通过 const& 选择 std::string 而不是 string_view 作为函数参数?

                  When would I choose std::string by const& instead of string_view for function arguments?

                  需要一个以空字符结尾的字符串吗?如果是这样,那么您应该使用 std::string const& 来保证.string_view 不是 - 它只是一个 const char 的范围.

                  Do you need a null-terminated string? If so, then you should use std::string const& which gives you that guarantee. string_view does not - it's simply a range of const char.

                  如果您不需要一个以空字符结尾的字符串,并且您不需要取得数据的所有权,那么您应该使用string_view.如果您确实需要获得数据的所有权,那么 string 按值可能比 string_view 更好.

                  If you do not need a null-terminated string, and you do not need to take ownership of the data, then you should use string_view. If you do need to take ownership of the data, then it may be the case that string by value is better than string_view.

                  这篇关于我什么时候会通过 const&amp;std::string 而不是 std::string_view?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

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

                      <bdo id='HQ3Dr'></bdo><ul id='HQ3Dr'></ul>
                    • <legend id='HQ3Dr'><style id='HQ3Dr'><dir id='HQ3Dr'><q id='HQ3Dr'></q></dir></style></legend>

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