Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a data structure that doesn't allow duplicates and also maintains order of entry?

Duplicate: Choosing a STL container with uniqueness and which keeps insertion ordering

I'm looking for a data structure that acts like a set in that it doesn't allow duplicates to be inserted, but also knows the order in which the items were inserted. It would basically be a combination of a set and list/vector.

I would just use a list/vector and check for duplicates myself, but we need that duplicate verification to be fast as the size of the structure can get quite large.

like image 908
Nick McCowin Avatar asked Apr 30 '09 17:04

Nick McCowin


People also ask

What data structure does not allow duplicates?

HashSet, LinkedHashSet and TreeSet are the implementations of Set interface which does not allow duplicate elements. In this tutorial we will see the differences between them. Fail-Fast Iterator is returned by HashSet, LinkedHashSet and TreeSet.

Which data structure does not allow duplicate values in C++?

Unordered sets do not allow duplicates and are initialized using comma-delimited values enclosed in curly braces.

Which data structure can have duplicates?

Summary. a List object can contain duplicate elements.

Which containers do not allow duplicates?

So, the conclusion, use std::unordered_set or std::unordered_map (if you need the key-value feature). And you don't need to check before doing the insertion, these are unique-key containers, they don't allow duplicates.


2 Answers

Take a look at Boost.MultiIndex. You may have to write a wrapper over this.

like image 121
dirkgently Avatar answered Nov 14 '22 23:11

dirkgently


A Boost.Bimap with the insertion order as an index should work (e.g. boost::bimap < size_t, Foo > ). If you are removing objects from the data structure, you will need to track the next insertion order value separately.

like image 24
equackenbush Avatar answered Nov 14 '22 22:11

equackenbush