std::unique が呼び出された後、ベクトル内のポインターを削除するにはどうすればよいですか?
例えば:
struct Foo
{
Foo(int bar) : mBar(bar) {}
~Foo() { std::cout << "~dtor\n"; }
int mBar;
};
bool SortFunc(Foo * right, Foo * left) { return right->mBar < left->mBar; }
// Should I 'delete left;' in case of equality?
bool CompareFunc(Foo * right, Foo * left)
{
return right->mBar == left->mBar;
}
// NOTE: In my code, vector is initialized in another class which I cannot modify.
void InitializeList(std::vector<Foo *> & fooList)
{
Foo * firstFoo = new Foo(1);
Foo * secondFoo = new Foo(2);
// This pointer will not be in vector anymore after std::unique is called!
Foo * thirdFoo = new Foo(1);
Foo * forthFoo = new Foo(4);
fooList.push_back(firstFoo);
fooList.push_back(secondFoo);
fooList.push_back(thirdFoo);
fooList.push_back(forthFoo);
}
int main()
{
{ // Block exists to see if Foo::dtor is called.
std::vector<Foo *> fooList;
InitializeList(fooList);
std::sort(fooList.begin(), fooList.end(), SortFunc);
std::vector<Foo *>::iterator itrResult = fooList.end();
// Pointer to thirdFoo is dangling after std::unique is called.
itrResult = std::unique(fooList.begin(), fooList.end(), CompareFunc);
fooList.erase(itrResult, fooList.end());
// ... Other operations and clean up code.
}
return 0;
}