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

โปรแกรม C++ สำหรับการใช้งาน Set ใน STL

Set เป็นชนิดข้อมูลนามธรรม (abstract data type) ที่องค์ประกอบแต่ละตัวต้องมีค่าไม่ซ้ำกัน (unique) เนื่องจากค่าขององค์ประกอบเป็นตัวระบุตัวตนเอง ค่าขององค์ประกอบไม่สามารถแก้ไขได้หลังเพิ่มเข้าเซตแล้ว แต่สามารถลบออกและเพิ่มค่าใหม่ที่แก้ไขแล้วได้

ฟังก์ชันและคำอธิบาย

  • st.size() – คืนขนาดของเซต
  • st.insert() – ใช้แทรกองค์ประกอบเข้าเซต
  • st.erase() – ลบองค์ประกอบออกจากเซต
  • st.find() – คืน iterator ไปยังองค์ประกอบที่ค้นหา หากพบ มิฉะนั้นคืน iterator ไปยัง end
  • st.begin() – คืน iterator ไปยังองค์ประกอบแรก
  • st.end() – คืน iterator ไปยังตำแหน่งหลังองค์ประกอบสุดท้าย

โค้ดตัวอย่าง (โปรแกรมโต้ตอบแบบเมนู)

#include <iostream>
#include <set>
#include <string>
#include <cstdlib>
using namespace std;

int main() {
    set<int> st;
    set<int>::iterator it;
    int c, i;

    while (true) {
        cout << "1.Size of the Set" << endl;
        cout << "2.Insert Element into the Set" << endl;
        cout << "3.Delete Element of the Set" << endl;
        cout << "4.Find Element in a Set" << endl;
        cout << "5.Display the set" << endl;
        cout << "6.Exit" << endl;
        cout << "Enter your Choice: ";
        cin >> c;

        switch(c) {
            case 1:
                cout << "Size of the Set: " << st.size() << endl;
                break;
            case 2:
                cout << "Enter value to be inserted: ";
                cin >> i;
                st.insert(i);
                break;
            case 3:
                cout << "Enter the element to be deleted: ";
                cin >> i;
                st.erase(i);
                break;
            case 4:
                cout << "Enter the element to be found: ";
                cin >> i;
                it = st.find(i);
                if (it != st.end())
                    cout << "Element " << *it << " found in the set" << endl;
                else
                    cout << "No Element Found" << endl;
                break;
            case 5:
                cout << "Displaying Set by Iterator: ";
                for (it = st.begin(); it != st.end(); it++)
                    cout << *it << " ";
                cout << endl;
                break;
            case 6:
                exit(0);
            default:
                cout << "Wrong Choice" << endl;
        }
    }
    return 0;
}

ตัวอย่างผลลัพธ์

1.Size of the Set
2.Insert Element into the Set
3.Delete Element of the Set
4.Find Element in a Set
5.Display the set
6.Exit
Enter your Choice: 1
Size of the Set: 0
Enter your Choice: 2
Enter value to be inserted: 1
Enter your Choice: 2
Enter value to be inserted: 7
Enter your Choice: 2
Enter value to be inserted: 6
Enter your Choice: 2
Enter value to be inserted: 4
Enter your Choice: 3
Enter the element to be deleted: 1
Enter your Choice: 4
Enter the element to be found: 7
Element 7 found in the set
Enter your Choice: 6
Exit code: 0

คุณสมบัติสำคัญ: Set จัดเก็บค่าไม่ซ้ำ และจัดเรียงอัตโนมัติจากน้อยไปมาก การค้นหา แทรก ลบ มีความซับซ้อน O(log n)