什么是stl集合的C#等价物? [英] What is the C# equivalent of the stl set?

查看:115
本文介绍了什么是stl集合的C#等价物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C#在一个平衡的二叉搜索树中存储一些值。我查看了泛型名称空间中的集合,我没有找到等价的stl集。



我可以使用什么通用集合? (我不想存储键/值对...只是值。)

解决方案


  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> ; 。它是一个红黑树,仅存储值。



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.

这篇关于什么是stl集合的C#等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆