📌  相关文章
📜  C#|检查SortedSet是否为指定集合的超集

📅  最后修改于: 2021-05-30 00:43:06             🧑  作者: Mango

SortedSet类按排序顺序表示对象的集合。此类位于System.Collections.Generic命名空间下。 SortedSet .IsSupersetOf(IEnumerable )方法用于检查SortedSet 对象是否为指定集合的超集。

特性:

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

句法:

mySet1.IsSupersetOf(mySet2);

在这里, mySet1mySet2是SortedSets的两个对象。

返回值:如果SortedSet < T >对象是其他对象的超集,则此方法返回True ,否则返回false

异常:如果SortedSet为null,则此方法将提供ArgumentNullException。

下面的程序说明了SortedSet .IsSupersetOf(IEnumerable )方法的用法:

范例1:

// C# code to Check if a SortedSet is a
// superset of the specified collection
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedSet of strings
        SortedSet mySet1 = new SortedSet();
  
        // Inserting elements in SortedSet
        mySet1.Add("Geeks");
        mySet1.Add("GeeksQuiz");
  
        // Creating a SortedSet of strings
        SortedSet mySet2 = new SortedSet();
  
        // Inserting elements in SortedSet
        mySet2.Add("DS");
        mySet2.Add("C++");
        mySet2.Add("Java");
        mySet2.Add("JavaScript");
        mySet2.Add("GeeksQuiz");
        mySet2.Add("Geeks");
  
        // Check if a SortedSet is a superset
        // of the specified collection
        // It should return true as SortedSet mySet2
        // is superset of SortedSet mySet1
        Console.WriteLine(mySet2.IsSupersetOf(mySet1));
    }
}
输出:
True

范例2:

// C# code to Check if a SortedSet is a
// superset of the specified collection
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedSet of integers
        SortedSet mySet1 = new SortedSet();
  
        // Inserting elements in SortedSet
        mySet1.Add(2);
        mySet1.Add(3);
        mySet1.Add(4);
        mySet1.Add(5);
  
        // 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 superset
        // of the specified collection
        // It should return false as SortedSet mySet2
        // is not a superset of SortedSet mySet1
        Console.WriteLine(mySet2.IsSupersetOf(mySet1));
    }
}
输出:
False

参考:

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