<tfoot id='DphaS'></tfoot>

  • <legend id='DphaS'><style id='DphaS'><dir id='DphaS'><q id='DphaS'></q></dir></style></legend>
        <bdo id='DphaS'></bdo><ul id='DphaS'></ul>
    1. <small id='DphaS'></small><noframes id='DphaS'>

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

        为什么可以从函数返回“向量"?

        时间:2023-09-16
          <tbody id='VCfY2'></tbody>

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

                <tfoot id='VCfY2'></tfoot>
              1. <i id='VCfY2'><tr id='VCfY2'><dt id='VCfY2'><q id='VCfY2'><span id='VCfY2'><b id='VCfY2'><form id='VCfY2'><ins id='VCfY2'></ins><ul id='VCfY2'></ul><sub id='VCfY2'></sub></form><legend id='VCfY2'></legend><bdo id='VCfY2'><pre id='VCfY2'><center id='VCfY2'></center></pre></bdo></b><th id='VCfY2'></th></span></q></dt></tr></i><div id='VCfY2'><tfoot id='VCfY2'></tfoot><dl id='VCfY2'><fieldset id='VCfY2'></fieldset></dl></div>
              2. <legend id='VCfY2'><style id='VCfY2'><dir id='VCfY2'><q id='VCfY2'></q></dir></style></legend>
                  <bdo id='VCfY2'></bdo><ul id='VCfY2'></ul>
                  本文介绍了为什么可以从函数返回“向量"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  请考虑此代码.我已经多次看到这种类型的代码.words 是一个局部向量.如何从函数中返回它?

                  Please consider this code. I have seen this type of code several times. words is a local vector. How is it possible to return it from a function?

                  我们能保证它不会死吗?

                  Can we guarantee it will not die?

                   std::vector<std::string> read_file(const std::string& path)
                   {
                      std::ifstream file("E:\names.txt");
                  
                      if (!file.is_open())
                      {
                          std::cerr << "Unable to open file" << "
                  ";
                          std::exit(-1);
                      }
                  
                      std::vector<string> words;//this vector will be returned
                      std::string token;
                  
                      while (std::getline(file, token, ','))
                      {
                          words.push_back(token);
                      }
                  
                      return words;
                  }
                  

                  推荐答案

                  我们能保证它不会死吗?

                  Can we guarantee it will not die?

                  只要没有返回引用,这样做就完全没问题.words 将移动到接收结果的变量中.

                  As long there is no reference returned, it's perfectly fine to do so. words will be moved to the variable receiving the result.

                  局部变量将超出范围.在移动(或复制)之后.

                  The local variable will go out of scope. after it was moved (or copied).

                  这篇关于为什么可以从函数返回“向量"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:标准对矢量调用 clear 如何改变容量有什么说法 下一篇:从头到尾迭代C++向量

                  相关文章

                  最新文章

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

                      <tfoot id='Musyh'></tfoot>

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