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

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

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

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

      反向迭代向量

      时间:2024-08-14

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

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

            • <legend id='zSREt'><style id='zSREt'><dir id='zSREt'><q id='zSREt'></q></dir></style></legend>
              • <bdo id='zSREt'></bdo><ul id='zSREt'></ul>
                本文介绍了反向迭代向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我需要从结尾到开头遍历一个向量.正确"的方式是

                I need to iterate over a vector from the end to the beginnig. The "correct" way is

                for(std::vector<SomeT>::reverse_iterator rit = v.rbegin(); rit != v.rend(); ++rit)
                {
                    //do Something
                }
                

                当doSomething涉及知道实际的索引时,那么需要用rit做一些计算才能得到它,比如index = v.size() - 1 - (rit - v.rbegin)

                When doSomething involves knowing the actual index, then some calculations need to be done with rit to obtain it, like index = v.size() - 1 - (rit - v.rbegin)

                如果无论如何都需要索引,那么我坚信最好使用该索引进行迭代

                If the index is needed anyway, then I strongly believe it is better to iterate using that index

                for(int i = v.size() - 1; i >= 0; --i)
                {
                    //do something with v[i] and i; 
                }
                

                这会警告 i 已签名且 v.size() 未签名.更改为

                This gives a warning that i is signed and v.size() is unsigned. Changing to

                for(unsigned i = v.size() - 1; i >= 0; --i) 只是功能错误,因为这本质上是一个无限循环 :)

                for(unsigned i = v.size() - 1; i >= 0; --i) is just functionally wrong, because this is essentially an endless loop :)

                做我想做的事情的美学上的好方法是什么

                What is an aesthetically good way to do what I want to do which

                • 无警告
                • 不涉及演员表
                • 不过分冗长

                我希望我不是在寻找不存在的东西:)

                I hope I am not looking for something that doesn't exist :)

                推荐答案

                如您所见,i >= 0 条件在未签名时的问题在于条件总是真的.不是在初始化 i 时减 1,然后在每次迭代后再次减 1,而是在检查循环条件后减 1:

                As you've noted, the problem with a condition of i >= 0 when it's unsigned is that the condition is always true. Instead of subtracting 1 when you initialize i and then again after each iteration, subtract 1 after checking the loop condition:

                for (unsigned i = v.size(); i-- > 0; )
                

                我喜欢这种风格有几个原因:

                I like this style for several reasons:

                • 尽管 i 会在循环结束时返回到 UINT_MAX,但它并不依赖这种行为——它会起作用如果类型已签名,则相同.依赖未签名的环绕对我来说有点像黑客.
                • 它只调用一次 size().
                • 它不使用 >=.每当我在 for 循环中看到该运算符时,我都必须重新阅读它以确保没有逐一错误.
                • 如果您更改条件中的间距,则可以使用 转到"运算符.
                • Although i will wrap around to UINT_MAX at the end of the loop, it doesn't rely on that behavior — it would work the same if the types were signed. Relying on unsigned wraparound feels like a bit of a hack to me.
                • It calls size() exactly once.
                • It doesn't use >=. Whenever I see that operator in a for loop, I have to re-read it to make sure there isn't an off-by-one error.
                • If you change the spacing in the conditional, you can make it use the "goes to" operator.

                这篇关于反向迭代向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:理解 STL 中的迭代器 下一篇:C++ deque:当迭代器失效时

                相关文章

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

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