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

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

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

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

      <tfoot id='vLVmL'></tfoot>
      1. C++ 向量,返回与参数

        C++ vector, return vs. parameter(C++ 向量,返回与参数)

            • <bdo id='4zgku'></bdo><ul id='4zgku'></ul>

                  <tbody id='4zgku'></tbody>
                <legend id='4zgku'><style id='4zgku'><dir id='4zgku'><q id='4zgku'></q></dir></style></legend>

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

                  <small id='4zgku'></small><noframes id='4zgku'>

                  本文介绍了C++ 向量,返回与参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  可能的重复:
                  如何返回对象"在 C++ 中

                  我想知道以下三种方法是否有区别:

                  I am wondering if there is a difference between the three following approaches:

                  void FillVector_1(vector<int>& v) {
                      v.push_back(1); // lots of push_backs!
                  }
                  
                  vector<int> FillVector_2() {
                      vector<int> v;
                      v.push_back(1); // lots of push_backs!
                      return v;
                  }
                  
                  vector<int> FillVector_3() {
                      int tab[SZ] = { 1, 2, 3, /*...*/ };
                      return vector<int>(tab, tab + SZ);
                  }
                  

                  推荐答案

                  最大的区别是第一种方式是附加到现有内容,而另外两种方式填充一个空向量.:)

                  The biggest difference is that the first way appends to existing contents, whereas the other two fill an empty vector. :)

                  我觉得你要找的关键词是返回值优化,应该比较常见(使用 G++ 你必须专门关闭它以防止它被应用).也就是说,如果用法是这样的:

                  I think the keyword you are looking for is return value optimization, which should be rather common (with G++ you'll have to turn it off specifically to prevent it from being applied). That is, if the usage is like:

                  vector<int> vec = fill_vector();
                  

                  那么可能很容易没有副本(而且该功能更易于使用).

                  then there might quite easily be no copies made (and the function is just easier to use).

                  如果您正在使用现有向量

                  If you are working with an existing vector

                  vector<int> vec;
                  while (something)
                  {
                      vec = fill_vector();
                      //do things
                  }
                  

                  然后使用 out 参数将避免在循环中创建向量和复制数据.

                  then using an out parameter would avoid creation of vectors in a loop and copying data around.

                  这篇关于C++ 向量,返回与参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                • <legend id='DwErj'><style id='DwErj'><dir id='DwErj'><q id='DwErj'></q></dir></style></legend>
                • <small id='DwErj'></small><noframes id='DwErj'>

                    <tbody id='DwErj'></tbody>

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