C # alternative for C ++ STL set <T>
I am looking for a sorted data structure that will look like a set of STL (T). I found a SortedList, but it requires (key, val), I'm looking for something like List (string) - just sorted.
I found Spring.Collections on the Internet, but my infrastructure does not recognize it.
Is there a simple SortedSet that I could use in a regular base structure?
Thanks Gal
You can do this with A System.Collections.Generic.Dictionary. Here is a good article: Dictionary and Sorting
Edit: SortedDictionary seems even better .
, SortedDictionary<K,V> SortedList<K,V>.
C5 Collections . : SortedArray<T>, TreeBag<T> TreeSet<T>.
Power Collections, OrderedBag<T> OrderedSet<T>.
List < > Sort?
,
public class SortedList<T>: List<T>
{
public SortedList(): base()
{
}
public SortedList(IEnumerable<T> collection): base(collection)
{
}
public SortedList(int capacity)
: base(capacity)
{
}
public void AddSort(T item)
{
base.Add(item);
this.Sort();
}
}
, AddSort.
List < > . Add .
, :
public static class ListExtension
{
public static void AddSort<T>(this List<T> list, T item)
{
list.Add(item);
list.Sort();
}
}
, :
List<int> newList = List<int>();
newList.AddSort(6);
newList.AddSort(4);
newList.AddSort(3);
:
newList [0] == 3 newList [1] == 4 newList [3] == 6
newList.Add, newList.AddSort