<bdo id='lbXZP'></bdo><ul id='lbXZP'></ul>
  • <tfoot id='lbXZP'></tfoot>

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

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

        有什么理由在 C++ 中用 for(;condition;) 替换 while(condition)?

        时间:2023-08-27

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

            <bdo id='ZvkGJ'></bdo><ul id='ZvkGJ'></ul>
              <tbody id='ZvkGJ'></tbody>

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

                <i id='ZvkGJ'><tr id='ZvkGJ'><dt id='ZvkGJ'><q id='ZvkGJ'><span id='ZvkGJ'><b id='ZvkGJ'><form id='ZvkGJ'><ins id='ZvkGJ'></ins><ul id='ZvkGJ'></ul><sub id='ZvkGJ'></sub></form><legend id='ZvkGJ'></legend><bdo id='ZvkGJ'><pre id='ZvkGJ'><center id='ZvkGJ'></center></pre></bdo></b><th id='ZvkGJ'></th></span></q></dt></tr></i><div id='ZvkGJ'><tfoot id='ZvkGJ'></tfoot><dl id='ZvkGJ'><fieldset id='ZvkGJ'></fieldset></dl></div>
                1. 本文介绍了有什么理由在 C++ 中用 for(;condition;) 替换 while(condition)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  看起来像

                  while( condition ) {
                      //do stuff
                  }
                  

                  完全等同于

                  for( ; condition; ) {
                      //do stuff
                  }
                  

                  是否有任何理由使用后者而不是前者?

                  Is there any reason to use the latter instead of the former?

                  推荐答案

                  据我所知,没有好的理由.您通过使用不增加任何内容的 for 循环故意误导人们.

                  There's no good reason as far as I know. You're intentionally misleading people by using a for-loop that doesn't increment anything.

                  更新:

                  根据 OP 对该问题的评论,我可以推测您如何在实际代码中看到这样的构造.我以前见过(并使用过)这个:

                  Based on the OP's comment to the question, I can speculate on how you might see such a construct in real code. I've seen (and used) this before:

                  lots::of::namespaces::container::iterator iter = foo.begin();
                  for (; iter != foo.end(); ++iter)
                  {
                      // do stuff
                  }
                  

                  但这就是我将事情排除在 for 循环之外的范围.也许您的项目曾经有一个看起来像这样的循环.如果在循环中间添加删除容器元素的代码,则可能必须仔细控制 iter 的递增方式.这可能导致代码如下所示:

                  But that's as far as I'll go with leaving things out of a for-loop. Perhaps your project had a loop that looked like that at one time. If you add code that removes elements of a container in the middle of the loop, you likely have to control carefully how iter is incremented. That could lead to code that looks like this:

                  for (; iter != foo.end(); )
                  {
                      // do stuff
                  
                      if (condition)
                      {
                          iter = foo.erase(iter);
                      }
                      else
                      {
                          ++iter;
                      }
                  }
                  

                  然而,这不是不花 5 秒时间将其更改为 while 循环的借口.

                  However, that's no excuse for not taking the five seconds needed to change it into a while-loop.

                  这篇关于有什么理由在 C++ 中用 for(;condition;) 替换 while(condition)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++无限循环 下一篇:while 循环中的 C++ 浮点精度

                  相关文章

                    • <bdo id='DTKQn'></bdo><ul id='DTKQn'></ul>
                  1. <tfoot id='DTKQn'></tfoot>

                  2. <legend id='DTKQn'><style id='DTKQn'><dir id='DTKQn'><q id='DTKQn'></q></dir></style></legend>

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

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