且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是stl集合的C#等价物?

更新时间:2022-10-15 08:40:38


  1. 如果您需要排序集,请使用 SortedDictionary< T,U> 。这是使用二叉搜索树来实现的。诚然,您将使用64位每个条目,因为您正在存储一个键值对。你可以这样写一个包装:

      class Set< T> :SortedDictionary< T,bool> 
    {
    public void Add(T item)
    {
    this.Add(item,true);
    }
    }


  2. 如果您不需要排序设置,请使用 HashSet< T>


  3. 否则,请查看 C5通用收藏库。特别是 TreeSet< T&gt ; 。它是一个红黑树,仅存储值。



I want to store some values in a balanced binary search tree using C#. I looked through the collections in the generics namespace and I haven't found an equivalent of the stl set.

What generic collection can I use? (I don't want to store key/value pairs... just values.)

  1. If you require sorted set, use SortedDictionary<T,U>. This is implemented using a binary search tree. Admittedly, you will be using 64-bits per entry because you are storing a key-value pair underneath. You can write a wrapper around it like this:

    class Set<T> : SortedDictionary<T, bool>
    {
        public void Add(T item)
        {
            this.Add(item, true);
        }
    }
    

  2. If you don't require a sorted set, use HashSet<T>.

  3. Otherwise, check out C5 Generic Collection Library. In particular TreeSet<T>. It is a red-black tree and only stores the values.