如以下代码。
我想将向量中的元素移到后面。
例如:[(1),2,3,4] -> [2,3,4,(1)]
但是,它会导致双重免费问题。这段代码的逻辑很简单。
我想我滥用了擦除功能。这是真的吗?有没有人告诉我细节?
感谢您的阅读。
这是输出:
*** Error in '/home/ubuntu/workspace/hello-cpp-world.cc.o': double free or corruption (out): 0x00000000016ffca0 ***
这是代码片段:
#include <iostream>
#include <vector>
int main() {
std::vector<int*> targets;
int* a = new int;
*a = 1;
int* b = new int;
*b = 2;
targets.push_back(a);
targets.push_back(b);
int i =0;
for (std::vector<int*>::iterator obj1 = targets.begin(); obj1 != targets.end(); i++)
{
if(i==0)
{
int* tmp = *obj1;
targets.push_back(tmp);
targets.erase(obj1);
}
else obj1++;
}
}