Aart Stuurman
Aart Stuurman

Reputation: 3628

Sorted data structure fast iteration, insertion, deletion by iterator

I am looking for a data structure that has the following properties:

Anything else is not relevant and will never be used.

After searching around for a while I was not able to find a data structure that follows these properties. A heap allows for fast insertion and removal(althought not by iterator per se), but is not easy to iterate in the required way.

I have also looked at a sorted vector. This has fast insertion and correct iteration, but deletion is pretty hard there.

I would say this is a pretty common data structure, even though I was not able to find a matching structure. Furthermore I think the fact that I always have an iterator when deleting might improve the performance.

I hope you can help me in the right direction.

Upvotes: 0

Views: 909

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118435

std::set or std::multiset is specified to have logarithmic complexity for insert(), and amortized constant complexity for deletion via an existing iterator.

Iterating over a set/multiset will iterate in sorted order.

Upvotes: 5

Related Questions