次のコードのように。
ベクター内の要素を後ろに移動したい。
例: [(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++;
}
}