可以从c++标准容器中选择boost ptr_array。

时间:2022-06-29 20:59:58

I'm restricted to use C++ stdlib and looking for same memory releasing behavior boost ptr_array. As you know this automatically release the memory for the contained pointer list.

我只能使用c++ stdlib并寻找相同的内存释放行为boost ptr_array。如您所知,这将自动释放所包含指针列表的内存。

Anyway to get the same behavior in C++11 or later containers?

如何在c++ 11或以后的容器中获得相同的行为?

2 个解决方案

#1


3  

You can use any container that supports move semantics together with unique or shared pointers: container<std::unique_ptr<T>>. In your case, the direct equivalent is std::array<std::unique_ptr<T>, n>

您可以使用任何支持移动语义的容器以及惟一的或共享的指针:container <:unique_ptr> >。在您的例子中,直接等价的是std::array , n>

#2


2  

The newly introduced smart pointers like std::shared_ptr are compatible with the standard containers. Use them as elements: std::vector<std::shared_ptr<MyType>>.

新引入的智能指针如std: shared_ptr与标准容器兼容。使用它们作为元素:std::vector >。

#1


3  

You can use any container that supports move semantics together with unique or shared pointers: container<std::unique_ptr<T>>. In your case, the direct equivalent is std::array<std::unique_ptr<T>, n>

您可以使用任何支持移动语义的容器以及惟一的或共享的指针:container <:unique_ptr> >。在您的例子中,直接等价的是std::array , n>

#2


2  

The newly introduced smart pointers like std::shared_ptr are compatible with the standard containers. Use them as elements: std::vector<std::shared_ptr<MyType>>.

新引入的智能指针如std: shared_ptr与标准容器兼容。使用它们作为元素:std::vector >。