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

C++ STL-এ Set বাস্তবায়ন: উনিক এলিমেন্টের কন্টেইনার

Set একটি অ্যাসোসিয়েটিভ কন্টেইনার যেখানে প্রতিটি উপাদান উনিক (unique) হয়। উপাদানের মানই তার শনাক্তকারী। একবার যোগ হলে উপাদানের মান পরিবর্তন করা যায় না, তবে পুরনোটি মুছে নতুন মান যোগ করা সম্ভব। সেটের উপাদানগুলো সর্টেড অর্ডারে থাকে।

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

  • st.size() – সেটের আকার রিটার্ন করে।
  • st.insert() – সেটে উপাদান যোগ করে (ডুপ্লিকেট হলে ইনসার্ট হয় না)।
  • st.erase() – নির্দিষ্ট উপাদান মুছে ফেলে।
  • st.find() – উপাদান খুঁজে পেলে ইটারেটর রিটার্ন করে, নাহলে end()।
  • st.begin() – প্রথম উপাদানের ইটারেটর।
  • st.end() – শেষ উপাদানের পরবর্তী পজিশনের ইটারেটর।

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

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

int main() {
    set<int> st;
    set<int>::iterator it;
    int choice, value;

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

        switch (choice) {
            case 1:
                cout << "Size of the Set: " << st.size() << endl;
                break;
            case 2:
                cout << "Enter value to be inserted: ";
                cin >> value;
                st.insert(value);
                break;
            case 3:
                cout << "Enter the element to be deleted: ";
                cin >> value;
                st.erase(value);
                break;
            case 4:
                cout << "Enter the element to be found: ";
                cin >> value;
                it = st.find(value);
                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: 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: 1

বিশেষত্ব: সেটে ডুপ্লিকেট ভ্যালু ইনসার্ট করলে সেটি উপেক্ষা করা হয়। সার্চ, ইনসার্ট, ডিলিট সব O(log n) কমপ্লেক্সিটিতে হয় (রেড-ব্ল্যাক ট্রি বাস্তবায়ন)।

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

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

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

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