std::set equivalent in C# ?

Started by
14 comments, last by osmanb 17 years, 10 months ago
Or just a simple sorted container, I don’t want an associative container. I see SortedList and SortedDictionary are both associative. And I don’t see any other sorted containers.
Advertisement
If you want a container that's sorted but not associative, use a List<T> and .Sort() as needed. If you want a container that's sorted, not associative, and won't allow duplicates, you need a Set. One example is here, and the corresponding generic extensions (by a different author) are here.

hope that helps,
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
Can't you just map your key to a null or empty type?
Quote:Original post by NotAYakk
Can't you just map your key to a null or empty type?


Given that SortedList sorts the keys, and not the values, I guess not.
A map "maps" the key to a value.

If the key maps to a null or empty type, then the values are empty types, and the keys are non-null.
This may be a silly suggestions but...

Couldn't you just use the object itself as both the key AND the value? That would also effectively give you a set.
I'm still not sure that he really wants a set (in the mathematical sense), since sets aren't ordered. Taken in context, there is no such thing as "the fourth element of a set", for instance.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
True, but when speaking of containers, doesn't "set" generally just mean that no element may be present more than one time?
Quote:Original post by kSquared
If you want a container that's sorted but not associative, use a List<T> and .Sort() as needed. If you want a container that's sorted, not associative, and won't allow duplicates, you need a Set. One example is here, and the corresponding generic extensions (by a different author) are here.



hope that helps,

I’ll check out both of those, however how stable/reliable are they?
Quote:Original post by smitty1276
This may be a silly suggestions but...

Couldn't you just use the object itself as both the key AND the value? That would also effectively give you a set.

Ok is this a good solution? Sounds like a waste of memory.


Quote:Original post by kSquared
I'm still not sure that he really wants a set (in the mathematical sense), since sets aren't ordered. Taken in context, there is no such thing as "the fourth element of a set", for instance.
What I want specifically is a container that guarantees O(log n) and doesn’t allow duplicates, and that isn’t associative.

Why isn’t this in the .Net library?
I swear for all the raving I hear about how wonderful and easy to use C# and .Net are... They have given me nothing but headaches.
O(log n) what? Sorting, searching, insertion, deletion? An array is O(1) lookup but O(n) inserting, for instance. You need to be more specific. You're right that a "set" in the mathematical sense does not exist in the .NET framework, though.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}

This topic is closed to new replies.

Advertisement