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

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

    1. 在为每个执行 a 时从 std::vector 中擦除?

      时间:2023-09-16

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

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

                本文介绍了在为每个执行 a 时从 std::vector 中擦除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                迭代的正确方法是使用迭代器.但是,我认为通过擦除,迭代器无效.

                The proper way to iterate is to use iterators. However, I think by erasing, the iterator is invalidated.

                基本上我想做的是:

                for(iterator it = begin; it != end; ++it)
                {
                    if(it->somecondition() )
                    {
                     erase it
                    }
                
                }
                

                如果没有 v[i] 方法,我怎么能做到这一点?

                How could I do this without v[i] method?

                谢谢

                struct RemoveTimedEvent
                {
                    bool operator()(const AguiTimedEvent& pX, AguiWidgetBase* widget) const 
                    {
                        return pX.getCaller() == widget;
                    }
                };
                
                void AguiWidgetContainer::clearTimedEvents( AguiWidgetBase* widget )
                {
                    std::vector<AguiTimedEvent>::iterator it = std::remove_if(timedEvents.begin(),
                        timedEvents.end(), RemoveTimedEvent());
                    timedEvents.erase(it, timedEvents.end());
                
                }
                

                推荐答案

                erase() 返回一个新的迭代器:

                erase() returns a new iterator:

                for(iterator it = begin; it != end(container) /* !!! */;)
                {
                    if (it->somecondition())
                    {
                        it = vec.erase(it);  // Returns the new iterator to continue from.
                    }
                    else
                    {
                        ++it;
                    }
                }
                

                请注意,我们不能再将它与预先计算的结束进行比较,因为我们可能会删除它并因此使其无效.我们每次都必须明确地结束.

                Note that we can no longer compare it against a precalculated end, because we may erase it and therefore invalidate it. We must get the end explicitly each time.

                更好的方法可能是结合 std::remove_iferase().你从 O(N2)(每个元素都会被擦除并随着你的移动而移动)变成 O(N):

                A better method might be to combine std::remove_if and erase(). You change from being O(N2) (every element gets erased and shifted as you go) to O(N):

                iterator it = std::remove_if(begin, end, pred);
                vec.erase(it, vec.end());
                

                其中 pred 是您的移除谓词,例如:

                Where pred is your removal predicate, such as:

                struct predicate // do choose a better name
                {
                    bool operator()(const T& pX) const // replace T with your type
                    {
                        return pX.shouldIBeRemoved();
                    }
                };
                
                iterator it = std::remove_if(begin, end, predicate());
                vec.erase(it, vec.end());
                

                在你的情况下,你可以让它变得非常通用:

                In your case, you can make it pretty general:

                class remove_by_caller
                {
                public:
                    remove_by_caller(AguiWidgetBase* pWidget) :
                    mWidget(pWidget)
                    {}
                
                    // if every thing that has getCaller has a base, use that instead
                    template <typename T> // for now a template
                    bool operator()(const T& pX) const
                    {
                        return pX.getCaller() == mWidget;
                    }
                
                private:
                    AguiWidgetBase* mWidget;
                };
                
                std::vector<AguiTimedEvent>::iterator it =
                    std::remove_if(timedEvents.begin(), timedEvents.end(), remove_by_caller(widget));
                timedEvents.erase(it, timedEvents.end());
                

                注意 lambda 的存在是为了简化这个过程,在 Boost 和 C++11 中都是如此.

                Note lambda's exist to simplify this process, both in Boost and C++11.

                这篇关于在为每个执行 a 时从 std::vector 中擦除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:转换向量<int>到一个字符串 下一篇:向量、结构和 std::find

                相关文章

                最新文章

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

              • <legend id='geIkL'><style id='geIkL'><dir id='geIkL'><q id='geIkL'></q></dir></style></legend>
                  <bdo id='geIkL'></bdo><ul id='geIkL'></ul>

                <tfoot id='geIkL'></tfoot>

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