简体   繁体   English

将元素从向量复制到另一个

[英]copying elements from vector to another one

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. 我有两个指针向量: arr已经包含一些元素和temp ,我想将特定元素从arr复制到的新向量。 For example I'd like to copy second element of arr to be copied into temp and deleted from arr . 例如,我想将arr第二个元素复制到temp并从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();
}

您不应该delete要复制的对象,因为您希望将其保持在temp -只需将其从arr erase

arr.begin() gives you an iterator pointing to the first element, so if you want the second element, you should advance i one: arr.begin()为您提供了一个指向第一个元素的迭代器,因此,如果您想要第二个元素,则应将i提前一个:

++i;

If you want the index th element, you should advance i by index : 如果想要第index个元素,则应按index向前移动i

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. 在另一个答案中提到了molbdnilo屁股,您实际上有一个内存处理错误,因为您正在删除temp有指针的对象。

From what I understand you want to copy an element at specified index (argument index ) and remove it from vector temp . 据我了解,您想复制指定索引处的元素(参数index ),并将其从向量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; 还要注意,在函数中,您调用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. 这将释放i所存储的对象所在的内存,并且存储在arr的指针将变为无效( 悬空指针 ),因为它指向已释放的内存。

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? ) 如果有充分的理由为什么要使用指针向量( 在C ++中何时使用指针 ),我建议您改用智能指针向量:如果您支持TR1或C ++ 11,请使用std::shared_ptr ,否则使用boost::shared_ptrboost::shared_ptr 在哪里?

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. 这会将您要删除的所有元素复制到tmp中。 The to actually remove them from arr it's just a simple application of the remove-erase idiom: 要将其从arr实际删除,这只是remove-erase习惯用法的简单应用:

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. 可能有一种方法可以将这两个步骤合并为一行,但是您冒着失去可读性的风险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM