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

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

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

    <tfoot id='kf3Aj'></tfoot>

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

      1. C++ 向量,返回与参数

        时间:2023-10-07
            <bdo id='kPKwS'></bdo><ul id='kPKwS'></ul>

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

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

                <tfoot id='kPKwS'></tfoot>
              • <legend id='kPKwS'><style id='kPKwS'><dir id='kPKwS'><q id='kPKwS'></q></dir></style></legend>

                  本文介绍了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++ 向量,返回与参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:根据两个值对 STL 向量进行排序 下一篇:将元素添加到抽象类的 std::vector

                  相关文章

                  最新文章

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

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

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