Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C++

C++ STL में Multiset का कार्यान्वयन

मल्टीसेट एक प्रकार का सहयोगी कंटेनर है जिसमें कई तत्वों के समान मान हो सकते हैं।

फंक्शन और विवरण:

  • ms.size() – मल्टीसेट का आकार लौटाता है।
  • ms.insert() – मल्टीसेट में तत्व डालने के लिए उपयोग किया जाता है।
  • ms.erase() – मल्टीसेट से मान हटाता है।
  • ms.find() – यदि मल्टीसेट में खोजा गया तत्व मिलता है तो उसका इटरेटर लौटाता है, अन्यथा end इटरेटर लौटाता है।
  • 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 c, i;

    while (1) {
        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;
                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);
                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(1);
                break;
            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: 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: 1
  1. C++ STL में Set_Symmetric_Difference का कार्यान्वयन C++ STL में Set_Symmetric_Difference का कार्यान्वयन

    यह C++ प्रोग्राम set_symmetric_difference को लागू करने के लिए है। दो सेट का सिमेट्रिक डिफरेंस उन तत्वों द्वारा बनाया जाता है जो एक सेट में मौजूद होते हैं, लेकिन दूसरे में नहीं। सामान्य सेट ऑपरेशन हैं: Set Union Set Intersection Symmetric Set Difference या Exclusive-OR Set Difference या Subtraction

  1. C++ STL में Set_Intersection का कार्यान्वयन C++ STL में Set_Intersection का कार्यान्वयन

    दो सेट का इंटरसेक्शन केवल उन तत्वों द्वारा बनता है जो दोनों सेट में कॉमन होते हैं। फंक्शन द्वारा कॉपी किए गए तत्व हमेशा पहले सेट से उसी क्रम में आते हैं। दोनों सेट में तत्व पहले से क्रमबद्ध होने चाहिए। सामान्य सेट ऑपरेशन हैं: Set Union Set Intersection Symmetric Set Difference या Exclusive-OR Set

  1. C++ STL में Set_Difference का कार्यान्वयन C++ STL में Set_Difference का कार्यान्वयन

    दो सेट का डिफरेंस केवल उन तत्वों द्वारा बनता है जो पहले सेट में मौजूद होते हैं, दूसरे सेट में नहीं। फंक्शन द्वारा कॉपी किए गए तत्व हमेशा पहले सेट से उसी क्रम में आते हैं। दोनों सेट में तत्व पहले से क्रमबद्ध होने चाहिए। सामान्य सेट ऑपरेशन हैं: Set Union Set Intersection Symmetric Set Difference या