Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there any possibility of iterating through SET (i,e reading elements from SET) on the basis of order of insertion of those elements into SET in C++? Using SET is requirement, I can't change that.

Usage of extra memory for storing element id or insertion sequence number etc will require a lot of memory. Solutions of this kind will not be helpful to me.

I read some where that in Objective-C, NSOrderedSet give this kind of functionality ( I do not know Objective-C, NSOrderedSet ). Can the similar functionality be achieved in C++?
Posted
Updated 27-May-14 0:03am
v2
Comments
CPallini 27-May-14 6:11am    
If you need a vector, why don't you use a vector?
Legor 27-May-14 7:04am    
It shouldn't be too hard to change from a set to a vector by the way.

If insertion order is your problem, you get that with std::vector at no cost, but not with std::set.

If memory is your problem, std::vector uses less memory than std::set

If performance is your problem, std::vector is faster than std::set

Is there even a single good reason for you to use std::set? No, requirements don't count. I said good reason!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-May-14 11:28am    
Yes, this is related to our recent discussion. I voted 5, and, for completeness of the answers, added the references to this discussion and explained the key of the problems with the set, in Solution 3.
—SA
Sergey Alexandrovich Kryukov 28-May-14 11:33am    
Unfortunately, the situation looks similar or worse than with the one with that recent question. And the situation is social; it's about "I cannot change it".
—SA
Please see the answers on this question page: Order of objects are changed for different runs of program?[^].

On that page, Stefan Lang was the one to suggest a way of defining the order per your requirements and I argued against using the set. If you use the set and at the same time care of the order of elements, it's a clear indication that you should use some other container. In a set, sorting/ordering is just the implementation detail used to improve performance. The fundamental mathematical notion of the set does not assume having any order, but in implementation ordering is unavoidable, as soon as you have a way to traverse the set of elements.

And let me express my doubt about "I cannot change it". The "requirements" can be the matter of mistake exactly as any other programming mistake. This is the most important skill of the software developer to turn down any requirements with mistake and reconcile the problems with people developing requirements. After all, don't forget that if the requirement mistake originate from someone else, you will be responsible for the final result. And that is fair enough: this is because you did not speak up. Don't do this mistake.

—SA
 
Share this answer
 
v2
No, unless you insert them already ordered.
See: "set at C++ reference"[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900