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

C++ STL Set में इन्सर्शन और डिलीशन

इन्सर्शन

STL सेट में इन्सर्शन insert() और emplace() ऑपरेशन द्वारा किया जा सकता है।

Insert(): Insert() का उपयोग सेट में एलिमेंट्स इन्सर्ट करने के लिए किया जाता है। इन्सर्ट ऑपरेशन एक ऑब्जेक्ट के संदर्भ को लेता है।

उपयोग किए गए फंक्शन्स की सूची:

  • st.size() = सेट का आकार लौटाता है।
  • st.insert() = सेट में एलिमेंट्स इन्सर्ट करने के लिए उपयोग किया जाता है।

उदाहरण कोड (Insert)

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

int main() {
    set<int> st;
    set<int>::iterator it;
    int c, i;
    while (1) {
        cout<<"1.Size of the Set"<<endl;
        cout<<"2.Insert Element into the Set"<<endl;
        cout<<"3.Display the set: "<<endl;
        cout<<"4.Exit"<<endl;
        cout<<"Enter your Choice: ";
        cin>>c;
        switch(c) {
            case 1:
                cout<<"Size of the Set: ";
                cout<<st.size()<<endl;
                break;
            case 2:
                cout<<"Enter value to be inserted: ";
                cin>>i;
                st.insert(i);
                break;
            case 3:
                cout<<"Displaying Set by Iterator: ";
                for (it = st.begin(); it != st.end(); it++) {
                    cout << (*it)<<" ";
                }
                cout<<endl;
                break;
            case 4:
                exit(1);
                break;
            default:
                cout<<"Wrong Choice"<<endl;
        }
    }
    return 0;
}

आउटपुट (Insert)

1.Size of the Set
2.Insert Element into the Set
3.Display the set:
4.Exit
Enter your Choice: 1
Size of the Set: 0
...
Enter your Choice: 2
Enter value to be inserted: 4
...
Displaying Set by Iterator: 4 6 8 10
...
Exit code: 1

Emplace()

Emplace ऑपरेशन का उपयोग सेट में एलिमेंट्स को इन-प्लेस इन्सर्ट करने के लिए भी किया जाता है। यह ऑब्जेक्ट की अनावश्यक कॉपी से बचाता है और इन्सर्ट ऑपरेशन की तुलना में अधिक कुशलता से इन्सर्शन करता है।

उपयोग किए गए फंक्शन्स की सूची:

  • st.size() = सेट का आकार लौटाता है।
  • st.emplace() = सेट में एलिमेंट्स इन्सर्ट करने के लिए उपयोग किया जाता है।

उदाहरण कोड (Emplace)

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

int main() {
    set<int> st;
    set<int>::iterator it;
    int c, i;
    while (1) {
        cout<<"1.Size of the Set"<<endl;
        cout<<"2.Insert Element into the Set"<<endl;
        cout<<"3.Display the set: "<<endl;
        cout<<"4.Exit"<<endl;
        cout<<"Enter your Choice: ";
        cin>>c;
        switch(c) {
            case 1:
                cout<<"Size of the Set: ";
                cout<<st.size()<<endl;
                break;
            case 2:
                cout<<"Enter value to be inserted: ";
                cin>>i;
                st.emplace(i);
                break;
            case 3:
                cout<<"Displaying Set by Iterator: ";
                for (it = st.begin(); it != st.end(); it++) {
                    cout << (*it)<<" ";
                }
                cout<<endl;
                break;
            case 4:
                exit(1);
                break;
            default:
                cout<<"Wrong Choice"<<endl;
        }
    }
    return 0;
}

आउटपुट (Emplace)

1.Size of the Set
2.Insert Element into the Set
3.Display the set:
4.Exit
Enter your Choice: 1
Size of the Set: 0
...
Displaying Set by Iterator: 4 6 7 8
...
Exit code: 1

डिलीशन

erase() फंक्शन का उपयोग करके, हम सेट से एलिमेंट्स को डिलीट कर सकते हैं इसके आर्गुमेंट का उल्लेख करके, या तो इसकी पोजीशन, इसका मान या संख्याओं की एक रेंज।

यहां उपयोग किए गए फंक्शन्स की सूची:

  • st.size() = सेट का आकार लौटाता है।
  • st.insert() = सेट में एलिमेंट्स इन्सर्ट करने के लिए उपयोग किया जाता है।
  • st.erase() = सेट से एलिमेंट डिलीट करने के लिए।

उदाहरण कोड (Deletion)

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

int main() {
    set<int> st;
    set<int>::iterator it;
    int c, i;
    while (1) {
        cout<<"1.Size of the Set"<<endl;
        cout<<"2.Insert Element into the Set"<<endl;
        cout<<"3.Delete Element from the Set"<<endl;
        cout<<"4.Display the set: "<<endl;
        cout<<"5.Exit"<<endl;
        cout<<"Enter your Choice: ";
        cin>>c;
        switch(c) {
            case 1:
                cout<<"Size of the Set: ";
                cout<<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<<"Displaying Set by Iterator: ";
                for (it = st.begin(); it != st.end(); it++) {
                    cout << (*it)<<" ";
                }
                cout<<endl;
                break;
            case 5:
                exit(1);
                break;
            default:
                cout<<"Wrong Choice"<<endl;
        }
    }
    return 0;
}

आउटपुट (Deletion)

1.Size of the Set
2.Insert Element into the Set
3.Delete Element from the Set
4.Display the set:
5.Exit
Enter your Choice: 1
Size of the Set: 0
...
Enter your Choice: 3
Enter the element to be deleted: 2
...
Displaying Set by Iterator: 1 3 4
...
Exit code: 1
  1. - C++ STL में Set बनाम Map

    Set एक अमूर्त डेटा प्रकार है जिसमें प्रत्येक एलिमेंट अद्वितीय होना चाहिए क्योंकि एलिमेंट का मान उसकी पहचान करता है। एलिमेंट का मान एक बार सेट में जोड़े जाने के बाद संशोधित नहीं किया जा सकता है, लेकिन उस एलिमेंट के संशोधित मान को हटाना और जोड़ना संभव है। एक Map एक सहयोगी कंटेनर है जो एलिमेंट्स को मै

  1. - C++ STL में set lower_bound() फ़ंक्शन

    C++ STL में Set lower_bound() फ़ंक्शन कंटेनर में उस एलिमेंट की ओर इंगित करने वाले इटरेटर को लौटाता है जो पैरामीटर में पास किए गए k के बराबर है। यदि k सेट कंटेनर में मौजूद नहीं है, तो फ़ंक्शन k से तुरंत अगले एलिमेंट की ओर इंगित करने वाले इटरेटर को लौटाता है जो k से थोड़ा बड़ा है। एल्गोरिथम Begin &nb

  1. - C++ STL में Set find() फ़ंक्शन

    C++ STL में Set find() फ़ंक्शन सेट कंटेनर में खोजे गए एलिमेंट के लिए एक इटरेटर लौटाता है। यदि एलिमेंट नहीं मिलता है तो इटरेटर सेट में अंतिम एलिमेंट के ठीक बाद की स्थिति की ओर इंगित करता है। एल्गोरिथम Begin    Define function printS() to print elements of set container.    initia