代码之家  ›  专栏  ›  技术社区  ›  Gal Goldman

C++ STL集<t>的C++替代

  •  3
  • Gal Goldman  · 技术社区  · 16 年前

    我正在寻找一个排序的数据结构,它将类似于STL集(T)。 我找到了sortedList,但它需要(key,val),我在寻找类似list(string)的东西——只排序。

    我在web spring.collections上找到了,但我的框架无法识别它。

    是否有一个简单的排序集可以在常规的基本框架中使用?

    谢谢, 女孩

    6 回复  |  直到 15 年前
        1
  •  7
  •   Stormenet    16 年前

    您可以使用System.Collections.Generic.Dictionary执行此操作。这是一篇好文章: Dictionarys and sorting

    编辑: 这部小说甚至 better .

        2
  •  5
  •   vzczc    15 年前

    .NET 4.0中引入的SortedSet是您要查找的内容,请参阅msdn here

        3
  •  1
  •   Nikos Steiakakis    16 年前

    阿尔索 List<T> 可以排序。默认情况下,它不会被排序,但您可以对它进行排序,即使您愿意,也可以使用自定义排序算法进行排序。

        4
  •  1
  •   LukeH    16 年前

    框架中除了 SortedDictionary<K,V> SortedList<K,V> .

    这个 C5 Collections 图书馆有几个分类收藏。根据您的具体要求,下面其中一个应该可以做到这一点: SortedArray<T> , TreeBag<T> TreeSet<T> .

    还有 Power Collections ,它提供 OrderedBag<T> OrderedSet<T> 收藏。

        5
  •  0
  •   florin    16 年前

    存在始终排序的System.Collections.SortedList或System.Collections.Generic.SortedList。 或者可以使用array.sort方法对ar定义的时刻进行时间排序。

        6
  •  -1
  •   ChrisBD    16 年前

    如何使用列表并调用排序方法?

    不是分机,但是试试这个

    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。

    将使用扩展方法更改列表<gt;。添加方法并在其结尾调用排序。

    使用扩展方法

    将以下内容放在代码可访问的命名空间中:

    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);
    

    数值为:

    NealList[ 0 ]=3 NealList[ 1 ]=4 NealList[ 3 ]=6

    您也可以使用newlist.add,然后在调用newlist.addsort时对列表进行排序。

    推荐文章