<tfoot id='VVZNa'></tfoot>
  • <small id='VVZNa'></small><noframes id='VVZNa'>

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

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

        递增迭代器:++it 比 it++ 更有效吗?

        时间:2024-05-11
        • <tfoot id='XneJm'></tfoot>

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

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

                <i id='XneJm'><tr id='XneJm'><dt id='XneJm'><q id='XneJm'><span id='XneJm'><b id='XneJm'><form id='XneJm'><ins id='XneJm'></ins><ul id='XneJm'></ul><sub id='XneJm'></sub></form><legend id='XneJm'></legend><bdo id='XneJm'><pre id='XneJm'><center id='XneJm'></center></pre></bdo></b><th id='XneJm'></th></span></q></dt></tr></i><div id='XneJm'><tfoot id='XneJm'></tfoot><dl id='XneJm'><fieldset id='XneJm'></fieldset></dl></div>
                  <bdo id='XneJm'></bdo><ul id='XneJm'></ul>
                    <tbody id='XneJm'></tbody>
                  本文介绍了递增迭代器:++it 比 it++ 更有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Possible Duplicate:
                  Is there a performance difference between i++ and ++i in C++?

                  I am writing a program where an iterator is used to loop through a std::vector. Somebody told me that doing ++it in the for statement leads to more efficient code. In other words, they are saying that:

                  for ( vector<string>::iterator it=my_vector.begin(); it != my_vector.end(); ++it )
                  

                  runs faster than

                  for ( vector<string>::iterator it=my_vector.begin(); it != my_vector.end(); it++ )
                  

                  Is this true? If it is, what is the reason behind the efficiency improvement? All it++/++it does is move the iterator to the next item in the vector, isn't it?

                  解决方案

                  The reason behind the preincrement being faster is that post-increment has to make a copy of the old value to return. As GotW #2 put it, "Preincrement is more efficient than postincrement, because for postincrement the object must increment itself and then return a temporary containing its old value. Note that this is true even for builtins like int."

                  GotW #55 provides the canonical form of postincrement, which shows that it has to do preincrement plus some more work:

                  T T::operator++(int)
                  {
                    T old( *this ); // remember our original value
                    ++*this;        // always implement postincrement
                                    //  in terms of preincrement
                    return old;     // return our original value
                  }
                  

                  As others have noted, it's possible for some compiler to optimize this away in some cases, but if you're not using the return value it's a good idea not to rely on this optimization. Also, the performance difference is likely to be very small for types which have trivial copy constructors, though I think using preincrement is a good habit in C++.

                  这篇关于递增迭代器:++it 比 it++ 更有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++,我可以在编译时静态初始化 std::map 吗? 下一篇:在输出之前按值对 std::map 进行排序 &amp;破坏

                  相关文章

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

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

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