📜  C#| SortedSet类

📅  最后修改于: 2021-05-29 13:22:37             🧑  作者: Mango

SortedSet类按排序顺序表示对象的集合。此类位于System.Collections.Generic命名空间下。

特征:

  • 在C#中,SortedSet类可用于存储,删除或查看元素。
  • 它保持升序,并且不存储重复的元素。
  • 如果必须存储唯一元素并保持升序,建议使用SortedSet类。

建设者

Constructor Description
SortedSet() Initializes a new instance of the SortedSet class.
SortedSet(IComparer) Initializes a new instance of the SortedSet class that uses a specified comparer.
SortedSet(IEnumerable) Initializes a new instance of the SortedSet class that contains elements copied from a specified enumerable collection.
SortedSet(IEnumerable, IComparer) Initializes a new instance of the SortedSet class that contains elements copied from a specified enumerable collection and that uses a specified comparer.
SortedSet(SerializationInfo, StreamingContext) Initializes a new instance of the SortedSet class that contains serialized data.

例子:

// C# code to create a SortedSet
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedSet of integers
        SortedSet mySortedSet = new SortedSet();
  
        // Adding elements in mySortedSet
        for (int i = 1; i <= 6; i++) {
            mySortedSet.Add(2 * i + 1);
        }
  
        // Displaying elements in mySortedSet
        Console.WriteLine("The elements in mySortedSet are : ");
  
        // Displaying the element in mySortedSet
        foreach(int i in mySortedSet)
        {
            Console.WriteLine(i);
        }
    }
}
输出:
The elements in mySortedSet are : 
3
5
7
9
11
13

特性

Property Description
Comparer Gets the IComparer object that is used to order the values in the SortedSet.
Count Gets the number of elements in the SortedSet.
Max Gets the maximum value in the SortedSet, as defined by the comparer.
Min Gets the minimum value in the SortedSet, as defined by the comparer.

例子:

// C# code to illustrate the properties
// of SortedSet Class
using System;
using System.Collections.Generic;
   
class GFG {
   
    // Driver code
    public static void Main()
    {
   
        // Creating a SortedSet of integers
        SortedSet mySortedSet = new SortedSet();
   
        // adding elements in mySortedSet
        mySortedSet.Add(1);
        mySortedSet.Add(2);
        mySortedSet.Add(3);
        mySortedSet.Add(4);
        mySortedSet.Add(5);
   
        // ------ Count property ----------
          
        // Displaying the number of elements in
        // the SortedSet using "Count" property
        Console.WriteLine("The number of elements in mySortedSet are: "
                                                + mySortedSet.Count);
                                                  
        // ---------- Min property ----------
        // Displaying the minimum value in the SortedSet
        Console.WriteLine("The minimum element in SortedSet is : "
                                                + mySortedSet.Min);
    }
}
输出:
The number of elements in mySortedSet are: 5
The minimum element in SortedSet is : 1

方法

Method Description
Add(T) Adds an element to the set and returns a value that indicates if it was successfully added.
Clear() Removes all elements from the set.
Contains(T) Determines whether the set contains a specific element.
CopyTo() Copies a portion or all of a SortedSet to a compatible one-dimensional array, starting at the beginning of the destination array or at a specified index.
CreateSetComparer() Returns an IEqualityComparer object that can be used to create a collection that contains individual sets.
CreateSetComparer(IEqualityComparer) Returns an IEqualityComparer object, according to a specified comparer, that can be used to create a collection that contains individual sets.
Equals(Object) Determines whether the specified object is equal to the current object.
ExceptWith(IEnumerable) Removes all elements that are in a specified collection from the current SortedSet object.
GetEnumerator() Returns an enumerator that iterates through the SortedSet.
GetHashCode() Serves as the default hash function.
GetObjectData(SerializationInfo, StreamingContext) Implements the ISerializable interface and returns the data that you must have to serialize a SortedSet object.
GetType() Gets the Type of the current instance.
GetViewBetween(T, T) Returns a view of a subset in a SortedSet.
IntersectWith(IEnumerable) Modifies the current SortedSet object so that it contains only elements that are also in a specified collection.
IsProperSubsetOf(IEnumerable) Determines whether a SortedSet object is a proper subset of the specified collection.
IsProperSupersetOf(IEnumerable) Determines whether a SortedSet object is a proper superset of the specified collection.
IsSubsetOf(IEnumerable) Determines whether a SortedSet object is a subset of the specified collection.
IsSupersetOf(IEnumerable) Determines whether a SortedSet object is a superset of the specified collection.
MemberwiseClone() Creates a shallow copy of the current Object.
OnDeserialization(Object) Implements the ISerializable interface, and raises the deserialization event when the deserialization is completed.
Overlaps(IEnumerable) Determines whether the current SortedSet object and a specified collection share common elements.
Remove(T) Removes a specified item from the SortedSet.
RemoveWhere(Predicate) Removes all elements that match the conditions defined by the specified predicate from a SortedSet.
Reverse() Returns an IEnumerable that iterates over the SortedSet in reverse order.
SetEquals(IEnumerable) Determines whether the current SortedSet object and the specified collection contain the same elements.
SymmetricExceptWith(IEnumerable) Modifies the current SortedSet object so that it contains only elements that are present either in the current object or in the specified collection, but not both.
ToString() Returns a string that represents the current object.
TryGetValue(T, T) Searches the set for a given value and returns the equal value it finds, if any.
UnionWith(IEnumerable) Modifies the current SortedSet object so that it contains all elements that are present in either the current object or the specified collection.

例子:

// C# code to illustrate the methods
// of SortedSet Class
using System;
using System.Collections.Generic;
   
class GFG {
   
    // Driver code
    public static void Main()
    {
   
        // Creating a SortedSet of integers
        SortedSet mySortedSet = new SortedSet();
   
        // adding elements in mySortedSet
        mySortedSet.Add(2);
        mySortedSet.Add(4);
        mySortedSet.Add(6);
        mySortedSet.Add(8);
        mySortedSet.Add(10);
   
        //-------- Remove Method --------
          
        // Removing element "4" if found
        mySortedSet.Remove(4);
   
        // Displaying the elements in mySortedSet
        foreach(int i in mySortedSet)
        {
            Console.WriteLine(i);
        }
   
        Console.WriteLine("After Using Method");
   
        // Removing element "14" if found
        mySortedSet.Remove(14);
   
        // Displaying the element in mySortedSet
        foreach(int i in mySortedSet)
        {
            Console.WriteLine(i);
        }
          
        // -------- IsSubsetOf Method --------
          
        // Creating a SortedSet of integers
        SortedSet mySet2 = new SortedSet();
   
        // Inserting elements in SortedSet
        mySet2.Add(3);
        mySet2.Add(4);
        mySet2.Add(5);
        mySet2.Add(6);
   
        // Check if a SortedSet is a subset
        // of the specified collection
        // It should return false as SortedSet mySet2
        // is not a subset of SortedSet mySet1
        Console.WriteLine(mySet2.IsSubsetOf(mySortedSet));
    }
}
输出:
2
6
8
10
After Using Method
2
6
8
10
False

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.sortedset-1?view=netframework-4.7.2