HashSet<T>.ExceptWith(IEnumerable<T>) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从当前 HashSet<T> 对象中移除指定集合中的所有元素。
public:
virtual void ExceptWith(System::Collections::Generic::IEnumerable<T> ^ other);
public:
void ExceptWith(System::Collections::Generic::IEnumerable<T> ^ other);
public void ExceptWith (System.Collections.Generic.IEnumerable<T> other);
abstract member ExceptWith : seq<'T> -> unit
override this.ExceptWith : seq<'T> -> unit
member this.ExceptWith : seq<'T> -> unit
Public Sub ExceptWith (other As IEnumerable(Of T))
参数
- other
- IEnumerable<T>
要从 HashSet<T> 对象中移除的项的集合。
实现
例外
other
为 null
。
示例
以下示例创建两 HashSet<T> 个具有重叠数据集的集合。 然后,使用 ExceptWith 方法从较大的集中删除较小的值范围。
static void Main()
{
HashSet<int>^ lowNumbers = gcnew HashSet<int>();
HashSet<int>^ highNumbers = gcnew HashSet<int>();
for (int i = 0; i < 6; i++)
{
lowNumbers->Add(i);
}
for (int i = 3; i < 10; i++)
{
highNumbers->Add(i);
}
Console::Write("lowNumbers contains {0} elements: ", lowNumbers->Count);
DisplaySet(lowNumbers);
Console::Write("highNumbers contains {0} elements: ", highNumbers->Count);
DisplaySet(highNumbers);
Console::WriteLine("highNumbers ExceptWith lowNumbers...");
highNumbers->ExceptWith(lowNumbers);
Console::Write("highNumbers contains {0} elements: ", highNumbers->Count);
DisplaySet(highNumbers);
}
/* This example provides output similar to the following:
* lowNumbers contains 6 elements: { 0 1 2 3 4 5 }
* highNumbers contains 7 elements: { 3 4 5 6 7 8 9 }
* highNumbers ExceptWith lowNumbers...
* highNumbers contains 4 elements: { 6 7 8 9 }
*/
HashSet<int> lowNumbers = new HashSet<int>();
HashSet<int> highNumbers = new HashSet<int>();
for (int i = 0; i < 6; i++)
{
lowNumbers.Add(i);
}
for (int i = 3; i < 10; i++)
{
highNumbers.Add(i);
}
Console.Write("lowNumbers contains {0} elements: ", lowNumbers.Count);
DisplaySet(lowNumbers);
Console.Write("highNumbers contains {0} elements: ", highNumbers.Count);
DisplaySet(highNumbers);
Console.WriteLine("highNumbers ExceptWith lowNumbers...");
highNumbers.ExceptWith(lowNumbers);
Console.Write("highNumbers contains {0} elements: ", highNumbers.Count);
DisplaySet(highNumbers);
void DisplaySet(HashSet<int> set)
{
Console.Write("{");
foreach (int i in set)
{
Console.Write(" {0}", i);
}
Console.WriteLine(" }");
}
/* This example provides output similar to the following:
* lowNumbers contains 6 elements: { 0 1 2 3 4 5 }
* highNumbers contains 7 elements: { 3 4 5 6 7 8 9 }
* highNumbers ExceptWith lowNumbers...
* highNumbers contains 4 elements: { 6 7 8 9 }
*/
Shared Sub Main()
Dim lowNumbers As HashSet(Of Integer) = New HashSet(Of Integer)()
Dim highNumbers As HashSet(Of Integer) = New HashSet(Of Integer)()
For i As Integer = 0 To 5
lowNumbers.Add(i)
Next i
For i As Integer = 3 To 9
highNumbers.Add(i)
Next i
Console.Write("lowNumbers contains {0} elements: ", lowNumbers.Count)
DisplaySet(lowNumbers)
Console.Write("highNumbers contains {0} elements: ", highNumbers.Count)
DisplaySet(highNumbers)
Console.WriteLine("highNumbers ExceptWith lowNumbers...")
highNumbers.ExceptWith(lowNumbers)
Console.Write("highNumbers contains {0} elements: ", highNumbers.Count)
DisplaySet(highNumbers)
End Sub
' This example provides output similar to the following:
' lowNumbers contains 6 elements: { 0 1 2 3 4 5 }
' highNumbers contains 7 elements: { 3 4 5 6 7 8 9 }
' highNumbers ExceptWith lowNumbers...
' highNumbers contains 4 elements: { 6 7 8 9 }
注解
方法 ExceptWith 等效于数学集减法。
此方法是 O (n
) 操作,其中 n
是 参数中的 other
元素数。