কম্পিউটার টিউটোরিয়াল

C++ STL-এ Multiset বাস্তবায়ন: ডুপ্লিকেট অনুমোদিত সেট

Multiset একটি অ্যাসোসিয়েটিভ কন্টেইনার যা set-এর মতো কিন্তু একই মানের একাধিক উপাদান সংরক্ষণ করতে দেয়। উপাদানগুলো সর্টেড অর্ডারে থাকে।

প্রধান ফাংশন ও বর্ণনা:

  • ms.size() – মাল্টিসেটের আকার রিটার্ন করে।
  • ms.insert() – মাল্টিসেটে উপাদান যোগ করে (ডুপ্লিকেট অনুমোদিত)।
  • ms.erase() – নির্দিষ্ট মানের সব উপাদান বা নির্দিষ্ট ইটারেটর মুছে ফেলে।
  • ms.find() – উপাদান খুঁজে পেলে ইটারেটর রিটার্ন করে (প্রথম匹配)।
  • ms.count() – নির্দিষ্ট মানের উপাদান সংখ্যা রিটার্ন করে।
  • ms.begin() / ms.end() – প্রথম এবং শেষের ইটারেটর।

উদাহরণ কোড: ইন্টারেক্টিভ মাল্টিসেট প্রোগ্রাম

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

int main() {
    multiset<int> ms;
    multiset<int>::iterator it, it1;
    int choice, value;

    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 >> choice;

        switch (choice) {
            case 1:
                cout << "Size of the Multiset: " << ms.size() << endl;
                break;
            case 2:
                cout << "Enter value to be inserted: ";
                cin >> value;
                if (ms.empty())
                    it1 = ms.insert(value);
                else
                    it1 = ms.insert(it1, value);  // হিন্ট দিয়ে দ্রুত ইনসার্ট
                break;
            case 3:
                cout << "Enter value to be deleted: ";
                cin >> value;
                ms.erase(value);  // এই মানের সব উপাদান মুছে যাবে
                break;
            case 4:
                cout << "Enter element to find: ";
                cin >> value;
                it = ms.find(value);
                if (it != ms.end())
                    cout << "Element found" << endl;
                else
                    cout << "Element not found" << endl;
                break;
            case 5:
                cout << "Enter element to be counted: ";
                cin >> value;
                cout << value << " appears " << ms.count(value) << " 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: 6
Elements of the Multiset: 1 2 3 4
...
Enter your Choice: 3
Enter value to be deleted: 4
...
Enter your Choice: 5
Enter element to be counted: 2
2 appears 1 times.
Enter your Choice: 7
Exit code: 1

সেট ও মাল্টিসেটের পার্থক্য: set-এ ডুপ্লিকেট অনুমোদিত নয়, multiset-এ অনুমোদিত। erase(value) কল করলে মাল্টিসেটে সেই মানের সব উপাদান মুছে যায়। নির্দিষ্ট একটি মুছে ফেলতে erase(iterator) ব্যবহার করুন।

  1. C++ STL-এ Set Union বাস্তবায়ন: দুটি সেটের ইউনিয়ন

  2. C++ STL-এ Set Symmetric Difference বাস্তবায়ন

  3. C++ STL-এ Set Intersection বাস্তবায়ন: দুটি সেটের অন্তঃসেট

  4. C++ STL-এ Set Difference বাস্তবায়ন: সেট বিয়োগ