>  기사  >  백엔드 개발  >  C#의 SortedSet 클래스

C#의 SortedSet 클래스

王林
王林앞으로
2023-08-26 09:29:171024검색

C# 中的 SortedSet 类

C#의 SortedSet 클래스는 정렬된 순서로 유지되는 개체 컬렉션을 나타냅니다.

다음은 SortedSet 클래스의 속성입니다. −

1Comparer SortedSet의 값을 정렬하는 데 사용되는 IComparer 개체.SortedSet의 요소 수를 가져옵니다.다음과 같이 SortedSet8742468051c85b06f0a0af9e3e506b5c에서 최대값을 가져옵니다. 비교자에 의해 정의됩니다.SortedSet8742468051c85b06f0a0af9e3e506b5c에서 최소값을 다음과 같이 가져옵니다. 비교기에 의해 정의됩니다. 일련 번호
Sr
2

Count

3

Max

4

Min

다음은 SortedSet 클래스의 몇 가지 메서드입니다.

메서드 및 설명

1Add(T) 에 요소를 추가하세요. 컬렉션을 구성하고 요소가 성공적으로 추가되었는지 여부를 나타내는 값을 반환합니다. 2
성공적으로 추가되었는지 여부를 나타냅니다.

Clear()

세트에서 모든 요소를 ​​제거합니다. 세트에 특정 요소가 포함되어 있는지 확인합니다.

4

CopyTo(T[]) 전체 SortedSet8742468051c85b06f0a0af9e3e506b5c를 호환되는 1차원 배열로 복사합니다. 대상 배열.

5

CopyTo(T[], Int32) 지정된 배열 인덱스에서 시작하여 전체 SortedSet8742468051c85b06f0a0af9e3e506b5c를 호환되는 1차원 배열로 복사합니다.

6

CopyTo(T[], Int32, Int32) 지정된 수의 요소를 복사합니다. SortedSet8742468051c85b06f0a0af9e3e506b5c에서 호환 가능한 1D 배열로 변환 지정된 배열 인덱스에서 시작하는 배열입니다.

7

CreateSetComparer() 다음에 사용할 수 있는 IEqualityComparer 개체를 반환합니다. 개별 컬렉션을 포함하는 컬렉션을 만듭니다.

Example

이제 몇 가지 예를 살펴보겠습니다. − SortedSet에 특정 요소가 포함되어 있는지 확인하려면 코드는 다음과 같습니다. − Live Demonstration
using System;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      SortedSet<string> set1 = new SortedSet<string>();
      set1.Add("CD");
      set1.Add("CD");
      set1.Add("CD");
      set1.Add("CD");
      Console.WriteLine("Elements in SortedSet1...");
      foreach (string res in set1) {
         Console.WriteLine(res);
      }
      Console.WriteLine("Does the SortedSet1 contains the element DE? = "+set1.Contains("DE"));
      SortedSet<string> set2 = new SortedSet<string>();
      set2.Add("BC");
      set2.Add("CD");
      set2.Add("DE");
      set2.Add("EF");
      set2.Add("AB");
      set2.Add("HI");
      set2.Add("JK");
      Console.WriteLine("Elements in SortedSet2...");
      foreach (string res in set2) {
         Console.WriteLine(res);
      }
      Console.WriteLine("SortedSet2 is a superset of SortedSet1? = "+set2.IsSupersetOf(set1));
   }
}
Output이렇게 하면 다음과 같은 출력이 생성됩니다. −

Elements in SortedSet1...
CD
Does the SortedSet1 contains the element DE? = False
Elements in SortedSet2...
AB
BC
CD
DE
EF
HI
JK
SortedSet2 is a superset of SortedSet1? = True

SortedSet를 순회하는 열거자를 얻으려면 코드는 다음과 같습니다. −

ExampleOnline Demo
using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      SortedSet<string> set1 = new SortedSet<string>();
      set1.Add("AB");
      set1.Add("BC");
      set1.Add("CD");
      set1.Add("EF");
      Console.WriteLine("Elements in SortedSet1...");
      foreach (string res in set1) {
         Console.WriteLine(res);
      }
      SortedSet<string> set2 = new SortedSet<string>();
      set2.Add("BC");
      set2.Add("CD");
      set2.Add("DE");
      set2.Add("EF");
      set2.Add("AB");
      set2.Add("HI");
      set2.Add("JK");
      Console.WriteLine("Elements in SortedSet2 (Enumerator for SortedSet)...");
      SortedSet<string>.Enumerator demoEnum = set2.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }
   }
}

Output

이렇게 하면 다음과 같은 출력이 생성됩니다. −

Elements in SortedSet1...
AB
BC
CD
EF
Elements in SortedSet2 (Enumerator for SortedSet)...
AB
BC
CD
DE
EF
HI
JK

위 내용은 C#의 SortedSet 클래스의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제