C++ std::unique_ptr with STL container

I got a piece of code which uses a std::set to keep a bunch of pointer.

I used this to be sure of that each pointer will present only once in my container.

Then, I heard about std::unique_ptr that ensure the pointer will exists only once in my entire code, and that's exactly what I need.

So my question is quite simple, should I change my container type to std::vector ? Or It won't changes anything leaving a std::set ?

Upvotes: 0

Views: 487

Answers (1)

Gem Taylor
Gem Taylor

Reputation: 5613

I think the job your set is doing is probably different to unique_ptr.

Your set is likely recording some events, and ensuring that only 1 event is recorded for each object that triggers, using these terms very loosely.
An example might be tracing through a mesh and recording all the nodes that are passed through.
The objects themselves already exist and are owned elsewhere.

The purpose of unique_ptr is to ensure that there is only one owner for a dynamically allocated object, and ensure automatic destruction of the object. Your objects already have owners, they don't need new ones!

Upvotes: 1

Related Questions