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

+3
source share
6 answers

You can do this with A System.Collections.Generic.Dictionary. Here is a good article: Dictionary and Sorting

Edit: SortedDictionary seems even better .

+7
source

A SortedSet <T> , .NET 4.0, , , . MSDN

+5

List<T> . , , , .

+1

System.Collections.SortedList System.Collections.Generic.SortedList, . Array.Sort ar .

0

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

-1
source

Source: https://habr.com/ru/post/1711719/


All Articles