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

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

Multiset เป็นประเภทของคอนเทนเนอร์แบบ associative ที่อนุญาตให้มีองค์ประกอบหลายตัวที่มีค่าเท่ากัน (duplicate values) ต่างจาก set ที่ต้อง unique

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

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

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

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

int main() {
    multiset<int> ms;
    multiset<int>::iterator it, it1;
    int c, i;

    while (true) {
        cout << "1.Size of the Multiset" << endl;
        cout << "2.Insert Element into the Multiset" << endl;
        cout << "3.Delete Element from the Multiset" << endl;
        cout << "4.Find Element in a Multiset" << endl;
        cout << "5.Count Elements with a specific key" << endl;
        cout << "6.Display Multiset" << endl;
        cout << "7.Exit" << endl;
        cout << "Enter your Choice: ";
        cin >> c;

        switch(c) {
            case 1:
                cout << "Size of the Multiset: " << ms.size() << endl;
                break;
            case 2:
                cout << "Enter value to be inserted: ";
                cin >> i;
                // Hint iterator เพื่อประสิทธิภาพ (optional)
                if (ms.empty())
                    it1 = ms.insert(i);
                else
                    it1 = ms.insert(it1, i);
                break;
            case 3:
                cout << "Enter value to be deleted: ";
                cin >> i;
                ms.erase(i); // ลบทุกตัวที่มีค่า i
                break;
            case 4:
                cout << "Enter element to find: ";
                cin >> i;
                it = ms.find(i);
                if (it != ms.end())
                    cout << "Element found" << endl;
                else
                    cout << "Element not found" << endl;
                break;
            case 5:
                cout << "Enter element to be counted: ";
                cin >> i;
                cout << i << " appears " << ms.count(i) << " times." << endl;
                break;
            case 6:
                cout << "Elements of the Multiset: ";
                for (it = ms.begin(); it != ms.end(); it++)
                    cout << *it << " ";
                cout << endl;
                break;
            case 7:
                exit(0);
            default:
                cout << "Wrong Choice" << endl;
        }
    }
    return 0;
}

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

1.Size of the Multiset
2.Insert Element into the Multiset
3.Delete Element from the Multiset
4.Find Element in a Multiset
5.Count Elements with a specific key
6.Display Multiset
7.Exit
Enter your Choice: 1
Size of the Multiset: 0
Enter your Choice: 2
Enter value to be inserted: 1
Enter your Choice: 2
Enter value to be inserted: 2
Enter your Choice: 2
Enter value to be inserted: 3
Enter your Choice: 2
Enter value to be inserted: 4
Enter your Choice: 6
Elements of the Multiset: 1 2 3 4
Enter your Choice: 3
Enter value to be deleted: 4
Enter your Choice: 4
Enter element to find: 1
Element found
Enter your Choice: 5
Enter element to be counted: 2
2 appears 1 times.
Enter your Choice: 7
Exit code: 0

ความแตกต่างจาก Set: Multiset อนุญาตค่าซ้ำได้ count() จะคืนจำนวนที่มากกว่า 1 ได้ erase(value) จะลบทุกตัวที่มีค่านั้น (ใช้ erase(iterator) ถ้าต้องการลบตัวเดียว)