English 中文(简体)
Ve 灭ing功能
原标题:Vector erase function deleting wrong object

我的矢量如下:

vector<Curve> workingSet;

课程设置是一门“名称”和一系列结构,由建筑商积极分配。

我有一条从病媒中删除2件(4件)物品的通道。

vector<Curve>::iterator it = workingSet.begin();

    //remove tbi s children from the working set
    for (  ;  it != workingSet.end();){
        if (it->thisName == tbi.childAName ||it->thisName == tbi.childBName)
            it= workingSet.erase(it);
        else
            ++it;
        }

当浮标达到“(it)”时,我可以看到,蒸发器在病媒中找到了2个曲线。 这样做是好的;我希望将治疗2从病媒中删除。

然后,被挖尸官带走(我在那里有一个断点),这大概应该破坏曲线2。 但是,当我看看看这 watch时,我可以看到,正在摧毁的曲线是四点。 其后,仲裁员按照要求着手删除物体中的阵列,并给民族解放军确定阵列点。

当该方案完成时,我可以看到,病媒2已经从阵列中去除,治疗4仍然在那里。 4个阵列点仍与以前一样位于同一地点,但记忆被分配,阵列有垃圾。

谁能说明为什么将治疗4放在我身上?

N.b. (1) There is a copy constructor for the curve class, which does a deep copy. N.b. (2) There is more to the class/program than I ve mentioned here

Btw,曲线2和4中的阵列点指不同地点,并含有不同的价值,据夸张。

Edit:我现在执行复印任务。 现在正确的项目似乎从病媒中消失,但错误的 des子仍然被称作! 然而,当四阵列曲线被夸大回时,仍然无动于衷。

最佳回答

当一个项目从病媒中消失时,其背后的所有要素都转移到前线,以填补空域。 如果您的汇编者不支持<代码>move,则通过复制所有内容而做,而病媒中的最后一个项目现已合并到其面前的项目上,是重复的,正在删除。

至少应该如何工作。

问题回答

在我看来,病媒:不能用当地制造的非边际数据种类的病媒来使用喷洒(不是在蒸汽上建造的物体,现在适当的名字越走了我)。 我有你描述的完全相同的行为,最后一项内容被两度破坏(特别是如果你的物体有被仲裁员释放的记忆危险),你所删除的内容永远不会被破坏。 我不知道为什么会发生这种情况,但听起来是一件pit事。

Here is one way to fix the problem:

#include <iostream>
#include <vector>
#include <memory>

using namespace std;

class MyClass 
{
   public:
    int *x;
    MyClass(int x)
    {
       cout << "MyClass Constructor " << x << endl;
       this->x = new int(x);
    }
    MyClass(const MyClass& obj)
    {
       this->x = new int(*obj.x);
       cout << "copy constructor " << *this->x << endl;
    }
    ~MyClass()
    {
       cout << "MyClass Destructor " << *x << endl;
       delete x;
    }
};
int main(int argc, char* argv[])
{
   // incorrect
   vector<MyClass> bad_vect;
   for(int i=0;i<3;i++){
      bad_vect.push_back(MyClass(i));
      // causes a bunch of copying to happen.
      // std::move does not seem to fix this either
      // but in the end everything gets constructed as we d like
   }
   cout << " ---- " << endl;
   bad_vect.erase(bad_vect.begin() + 1); // we expect this to remove item with x = 1 and destruct it.
   // but it does NOT do that, it does remove the item with x=1 from the vector
   // but it destructs the last item in the vector, with x=2, clearly
   // not what we want. The destructor for object with x=1 never gets called
   // and the destructor for the last item gets called twice.
   // The first time with x=2 and since that memory is freed, the 2nd time
   // it prints garbage. Strangely the double-free doesn t crash the prog
   // but I wouldn t count on that all the time.
   // Seems some parts of STL have pitfalls with locally constructed objects
   cout << " ------------ " << endl;
   // below fixes this
   vector<unique_ptr<MyClass> >vect;
   for(int i=0;i<3;i++){
      unique_ptr<MyClass> ptr(new MyClass(i));
      vect.push_back( move( ptr )); // move is required since unique_ptr can only have one owner
      // or the single one-liner below
      //vect.push_back( move( unique_ptr<MyClass>(new MyClass(i)) ));
   }
   // the above prints out MyClass Constructor 0,1,2, etc
   vect.erase(vect.begin() + 1); // remove the 2nd element, ie with x=1
   // the above prints out MyClass Destructor 1, which is what we want

   for(auto& v : vect){
     cout << *(v->x) << endl;
   } // prints out 0 and 2
   return 0; // since we re using smart pointers, the destructors for the
           // remaining 2 objects are called. You could use regular pointers
           // but you would have to manually delete everything. shared_ptr
           // also works and you don t need the move(), but with a bit more overhead
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签