0

I have two vectors of pointers: arr that contains some elements already and temp, the new vector I want to copy specific elements from arr to. For example I'd like to copy second element of arr to be copied into temp and deleted from arr. How can it be done?

I tried this, but it's not good:

void deleteobject(vector < figure3d *> &arr,int index,vector < figure3d *> &temp)
{
     vector < figure3d * > :: iterator i=arr.begin();
     temp.insert(temp.begin(),*i);
     delete *i;
     arr.erase(i);
     temp[0]->print();
}
LihO
  • 41,190
  • 11
  • 99
  • 167
Asaf Shay
  • 13
  • 1
  • 7

4 Answers4

1

You shouldn't delete the object you're copying since you want to keep it in temp - just erase it from arr.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

arr.begin() gives you an iterator pointing to the first element, so if you want the second element, you should advance i one:

++i;

If you want the indexth element, you should advance i by index:

i += index;

Storing raw pointers in a vector, and having to remember to delete them when removing them from the vector is a memory leak waiting to happen. And ass molbdnilo mentioned in another answer, you actually have a memory handling error since you're deleting the object that temp has a pointer to.

Kleist
  • 7,785
  • 1
  • 26
  • 30
0

From what I understand you want to copy an element at specified index (argument index) and remove it from vector temp. This function could look simple like this:

void deleteobject(std::vector<figure3d*> &arr, int index, std::vector<figure3d*> &temp)
{
    temp.insert(temp.begin(), arr[index]);
    arr.erase(arr.begin() + index);
}

In this case, you don't copy the object itself, but only a reference to it - thus you don't need to free the memory where this object is stored.

Also note that in your function you call delete *i; which frees the memory where the object pointed by i is stored - and the pointer that stored in arr becomes invalid (dangling pointer), because it points to memory that was freed already.

And I also suggest you to use vector of objects rather than vector of pointers to objects - although elements get copied, it's usually fast enough and there is no troublesome memory management connected with it. And in case there is a good reason why you use vector of pointers ( When to use pointers in C++ ), I suggest you to use vector of smart pointers instead: if you have TR1 or C++11 support, use std::shared_ptr, otherwise use boost::shared_ptr ( Where is shared_ptr? )

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
  • it work thanksssssssss but dont i hold a memory and not using it ? could it be a problem ? – Asaf Shay Jun 01 '12 at 22:38
  • @AsafShay: In this case, you don't copy the object itself, but only a reference to it - thus you don't need to free the memory where this object is stored. – LihO Jun 01 '12 at 22:45
  • i understand .tanks you all for the help.i dealing with that for hours .again tanks all – Asaf Shay Jun 01 '12 at 22:53
0

This looks like a job for !
something like this is what I would do:

Class removePredicate{
Public: 
    bool operator() (figure3d** item) {
       //remove logic goes here
    }
};

remove_copy_if(arr.begin(), arr.end(), back_inserter(tmp), not1(removePredicate));

This will copy all the elements that you want to remove into tmp. The to actually remove them from arr it's just a simple application of the remove-erase idiom:

erase(remove_if(arr.begin(), arr.end(), not1(removePredicate)), arr.end());

There's probably a way to combine these two steps into one line but you run the risk of losing readability at point.

Cerin
  • 63
  • 10