Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of std::set in C#?

Tags:

c#

I would like to have a container

StdStyleSet<A>

of

class A : IComparable<A> { ... }

which satisfies the properties of std::set. This would especially be:

  • The elements themselve are the keys
  • Automatically sorted on insertion
like image 434
Danvil Avatar asked Apr 17 '10 14:04

Danvil


1 Answers

SortedSet<T> : see documents - although technically, it's in the .NET Framework.

For earlier versions, you could use a HashSet and sort using LINQ, not ideal if you're primarily consuming the set in an ordered fashion. Alternatively you could use a SortedDictionary<TKey,TValue> with the value as Object and store your elements in the keys with nulls for the values.

like image 74
tvanfosson Avatar answered Oct 10 '22 05:10

tvanfosson