Hi,
Guys i just wanted to know whether set store elements in specific order or it contains unordered elements only. As per c++ specification
http://www.cplusplus.com/reference/set/set/?kw=set
Sets are containers that store unique elements following a specific order.
As per my knowledge they are not ordered in the sense that you can't enforce an arbitrary order by placing an arbitrary element in an arbitrary position. it is usually implemented as a red-black tree and i believe due to this it is not possible to place and arbitrary element in an arbitrary position. In java the Set interface does not provide any ordering guarantees.

As you say, in Java Set is an interface for Collections with no duplicates but no information about ordering, so that's completely different from C++ sets which are weakly ordered. (The Java SortedSet interface is one that has ordered elements.) However, some of the Java concrete classes that implement Set do also maintain and support ordering of their contents. You just have to look at their API doc.

Edited 10 Months Ago by JamesCherrill

To get arbitrary positioning, you'll probably need something like a vector, which is basically a dynamic array.

What are your requirements? Perhaps you'll be able to get better ideas.

Thanks for your answer. I just wanted to know why in c++ specification it is mentioned that set store unique elements following a specific order.

@jamesCHerril : sorting and ordering both are different things here. Here i am talking about inserting/deleting the element in particular order.

OK yes. The "specific order" can be defined by a comparison function (Java SortedSet, C++ Set) or by remembering the order/position of elements as they are added (Java List), which are indeed quite different.

This article has been dead for over six months. Start a new discussion instead.