Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

วิธีสร้าง SortedSet ใน C#

SortedSet<T> (ใน System.Collections.Generic) เป็นคอลเลกชันที่เก็บค่าที่ ไม่ซ้ำกัน (unique) และ เรียงลำดับอัตโนมัติ (sorted) ใช้ Red-Black Tree ด้านใน ให้การเพิ่ม/ลบ/ค้นหา ใน O(log n)

ตัวอย่างโค้ดที่ 1: สร้างและวนลูปด้วย Enumerator

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        SortedSet set1 = new SortedSet();
        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 set2 = new SortedSet();
        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.Enumerator demoEnum = set2.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            string res = demoEnum.Current;
            Console.WriteLine(res);
        }
    }
}

ผลลัพธ์

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

ตัวอย่างโค้ดที่ 2: ลบตามเงื่อนไขด้วย RemoveWhere

using System;
using System.Collections.Generic;

public class Demo
{
    private static bool Demo(int i)
    {
        return ((i % 10) == 0); // ลบตัวที่หาร 10 ลงตัว
    }

    public static void Main(string[] args)
    {
        SortedSet set1 = new SortedSet();
        set1.Add(200);
        set1.Add(215);
        set1.Add(310);
        set1.Add(500);
        set1.Add(600);

        Console.WriteLine("SortedSet elements...");
        foreach (int i in set1)
        {
            Console.WriteLine(i);
        }

        Console.WriteLine();
        set1.RemoveWhere(Demo);
        Console.WriteLine("SortedSet after removing some elements...");
        foreach (int i in set1)
        {
            Console.WriteLine(i);
        }
    }
}

ผลลัพธ์

SortedSet elements...
200
215
310
500
600
SortedSet after removing some elements...
215

คุณสมบัติและเมธอดสำคัญของ SortedSet<T>

Memberคำอธิบาย
Add(T)เพิ่มค่า (คืน false หากซ้ำ)
Remove(T)ลบค่า (คืน false หากไม่พบ)
RemoveWhere(Predicate<T>)ลบทุกตัวที่ตรงเงื่อนไข
Contains(T)ตรวจสอบว่ามีค่านั้นหรือไม่
Min / Maxค่าต่ำสุด/สูงสุด (O(1))
Countจำนวนสมาชิก
GetEnumerator()ได้ Enumerator สำหรับวนลูป
UnionWith, IntersectWith, ExceptWith, SymmetricExceptWithSet operations
IsSubsetOf, IsSupersetOf, IsProperSubsetOf, IsProperSupersetOf, Overlaps, SetEqualsความสัมพันธ์เซต

SortedSet vs HashSet vs List

คุณสมบัติSortedSet<T>HashSet<T>List<T>
Unique valuesYesYesNo (allowed duplicates)
SortedYes (always)NoNo (insertion order)
Add/Remove/ContainsO(log n)O(1)O(n) / O(1) at end
Min/MaxO(1)O(n)O(n)
Index accessNoNoO(1)

เมื่อไหร่ควรใช้ SortedSet

  • ต้องการค่าที่ไม่ซ้ำและเรียงลำดับตลอดเวลา
  • ต้องการ Min/Max บ่อย
  • ต้องการ set operations (union, intersect, etc.)
  • ไม่ต้องการเข้าถึงแบบ index