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

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

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

      1. C++ STL:STL 容器上的哪种迭代方法更好?

        C++ STL: Which method of iteration over a STL container is better?(C++ STL:STL 容器上的哪种迭代方法更好?)

          <legend id='6HnSt'><style id='6HnSt'><dir id='6HnSt'><q id='6HnSt'></q></dir></style></legend>

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

            • <tfoot id='6HnSt'></tfoot>
              • <bdo id='6HnSt'></bdo><ul id='6HnSt'></ul>

                    <tbody id='6HnSt'></tbody>

                  本文介绍了C++ STL:STL 容器上的哪种迭代方法更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这对你们中的一些人来说可能看起来很无聊,但是以下两种在 STL 容器上迭代的方法中哪一种更好?为什么?

                  This may seem frivolous to some of you, but which of the following 2 methods of iteration over a STL container is better? Why?

                  class Elem;
                  typedef vector<Elem> ElemVec;
                  ElemVec elemVec;
                  
                  // Method 0
                  for (ElemVec::iterator i = elemVec.begin(); i != elemVec.end(); ++i)
                  {
                      Elem& e = *i;
                      // Do something
                  }
                  
                  // Method 1
                  for (int i = 0; i < elemVec.size(); ++i)
                  {
                      Elem& e = elemVec.at(i);
                      // Do something
                  }
                  

                  方法 0 似乎更简洁的 STL,但方法 1 使用较少的代码实现了相同的效果.容器上的简单迭代是all出现在任何源代码中的地方.所以,我倾向于选择方法 1,它似乎减少了视觉混乱和代码大小.

                  Method 0 seems like cleaner STL, but Method 1 achieves the same with lesser code. Simple iteration over a container is what appears all over the place in any source code. So, I'm inclined to pick Method 1 which seems to reduce visual clutter and code size.

                  PS:我知道迭代器可以做的不仅仅是一个简单的索引.但是,请让回复/讨论集中在对容器的简单迭代上,如上所示.

                  PS: I know iterators can do much more than a simple index. But, please keep the reply/discussion focused on simple iteration over a container like shown above.

                  推荐答案

                  第一个版本适用于任何容器,因此在将任何容器作为参数的模板函数中更有用.可以想象,它的效率也略高一些,即使对于向量也是如此.

                  The first version works with any container and so is more useful in template functions that take any container a s a parameter. It is also conceivably slightly more efficient, even for vectors.

                  第二个版本仅适用于向量和其他整数索引容器.对于那些容器来说,它更符合习惯,对于 C++ 的新手来说很容易理解,并且如果您需要对索引做其他事情(这并不罕见)很有用.

                  The second version only works for vectors and other integer-indexed containers. It'd somewhat more idiomatic for those containers, will be easily understood by newcomers to C++, and is useful if you need to do something else with the index, which is not uncommon.

                  像往常一样,恐怕没有简单的这个更好"的答案.

                  As usual, there is no simple "this one is better" answer, I'm afraid.

                  这篇关于C++ STL:STL 容器上的哪种迭代方法更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
                    <tfoot id='mDjTY'></tfoot>

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

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

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

                            <tbody id='mDjTY'></tbody>
                          • <legend id='mDjTY'><style id='mDjTY'><dir id='mDjTY'><q id='mDjTY'></q></dir></style></legend>