C++ c+的自定义擦除+;集合或其他集合
我想这样做:C++ c+的自定义擦除+;集合或其他集合,c++,design-patterns,stl,c++14,C++,Design Patterns,Stl,C++14,我想这样做: std::set<my_type*> s; s.insert(new my_type(...)); ... s.erase(...); std::set s; s、 插入(新的my_类型(…); ... s、 删除(…); 其中集合的擦除将删除指针以避免内存泄漏 这是否可能是C++容器或适当的解决方案是对容器进行子类的分类并编写自己的擦除,或者使用某种智能指针方案? 声明为 SET/COD> >存储 MyOyType < /C> >,但您发送的是代码“MyOyTy
std::set<my_type*> s;
s.insert(new my_type(...));
...
s.erase(...);
std::set s;
s、 插入(新的my_类型(…);
...
s、 删除(…);
其中集合的擦除将删除指针以避免内存泄漏
这是否可能是C++容器或适当的解决方案是对容器进行子类的分类并编写自己的擦除,或者使用某种智能指针方案?
声明为<代码> SET/COD> >存储<代码> MyOyType < /C> >,但您发送的是代码“MyOyType *< /Cuth>”,因此代码不一致。在任何情况下,如果不需要堆分配,请不要使用它:std::set<my_type> s;
// Emplace improves on s.insert(my_type(...)); by allowing construction in place
// to minimize move/copy work
s.emplace(...args for my_type...);
...
s.erase(...);
智能指针有什么问题?智能指针没有什么问题,我想知道是否有一种方法需要更少的工程设计;s、 插入(使_唯一(…)代码>在我看来并不比你的代码更工程化。你是说
std::set s代码>,是吗?@user318904智能指针在c++14中,正如JohnB指出的那样,它几乎不是任何工程。对标准集合类进行子类化是非常困难的。
std::set<std::unique_ptr<my_type>> s;
s.insert(std::make_unique<my_type>(...));
...
s.erase(...);