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.

Recommended Answers

All 4 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.