1

Is there a unique container such like std::list with simple functionality (push,pop,clear, etc.) and not sorted order unlike std::set, or maybe I need to extend std::list and add my own push_unique method )) ?

4
  • 3
    You are perhaps looking for std::(tr1::)unordered_set. Commented Feb 12, 2013 at 13:38
  • 1
    Do you want the order of insertion to remain unchanged? Commented Feb 12, 2013 at 13:39
  • Do you need the container to retain the order of items pushed onto it? Commented Feb 12, 2013 at 13:44
  • Order doesn't matter, thanks Zack I will try Commented Feb 12, 2013 at 13:45

1 Answer 1

6

The STL is supposed to provide efficient containers.

A container which does not allow duplicates needs to support fast lookup to determine whether the value you want to is already present in the collection or not.

std::set keeps the item sorted in a red-black tree, which is what allows an O(log(n)) lookup, insertion, and removal.

std::unsorted_set allows constant-time lookup, insertion, and removal, but you need to provide a hash function for most UDT types, you need to take care of issues such as rehashing, which cause iterator invalidation, and you do not have any defined order for your items (not even the insertion order).

If you want to use a simple collection such as std::vector without allowing duplicates, you need to provide your own adapter.

However, I still can't figure out why you have problems with a sorted container such as std::set if, as you say, the order doesn't matter for you.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.