5

あるクラス T の std::list があるとしましょう。

これらの要素を管理する最善の方法は何ですか? リストからアイテムを追加または削除できるのはマネージャー(つまり、1人の所有者)だけであることを考慮してください。

1)

std::list < T* > myList;

//adding the new element
myList.push_back( new T(..) );

//deleting the one element
...roaming through the list...got it...
delete *iterator;
myList.erase(iterator);

2)

std::list < std::unique_ptr<T> > myList;

//adding the new element
myList.push_back ( std::unique_ptr<T>( new T(..) );

//deleting the one element
...roaming through the list...got it...
myList.erase(iterator);
4

2 に答える 2