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

एसटीएल सेट सी++ में सम्मिलन और हटाना

सम्मिलन

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

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

कार्यों की सूची का उपयोग किया जाता है:

  • st.size() =सेट का आकार लौटाता है।
  • st.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;
}

आउटपुट

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
1.Size of the Set
2.Insert Element into the Set
3.Display the set:
4.Exit

Enter your Choice: 2
Enter value to be inserted: 4
1.Size of the Set
2.Insert Element into the Set
3.Display the set:
4.Exit

Enter your Choice: 2
Enter value to be inserted: 6
1.Size of the Set
2.Insert Element into the Set
3.Display the set:
4.Exit

Enter your Choice: 2
Enter value to be inserted: 8
1.Size of the Set
2.Insert Element into the Set
3.Display the set:
4.Exit

Enter your Choice: 2
Enter value to be inserted: 10
1.Size of the Set
2.Insert Element into the Set
3.Display the set:
4.Exit

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

Exit code: 1

एम्प्लेस()

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

प्रयुक्त कार्यों की सूची:

  • st.size() =सेट का आकार लौटाता है।
  • st.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;
}

आउटपुट

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
1.Size of the Set
2.Insert Element into the Set
3.Display the set:
4.Exit
Enter your Choice: 2
Enter value to be inserted: 4
1.Size of the Set
2.Insert Element into the Set
3.Display the set:
4.Exit
Enter your Choice: 6
Wrong Choice
1.Size of the Set
2.Insert Element into the Set
3.Display the set:
4.Exit
Enter your Choice: 2
Enter value to be inserted: 6
1.Size of the Set
2.Insert Element into the Set
3.Display the set:
4.Exit
Enter your Choice: 2
Enter value to be inserted: 7
1.Size of the Set
2.Insert Element into the Set
3.Display the set:
4.Exit
Enter your Choice: 2
Enter value to be inserted: 8
1.Size of the Set
2.Insert Element into the Set
3.Display the set:
4.Exit
Enter your Choice: 3
Displaying Set by Iterator: 4 6 7 8
1.Size of the Set
2.Insert Element into the Set
3.Display the set:
4.Exit
Enter your Choice: 4

Exit code: 1

हटाना

इरेज़ () फ़ंक्शन का उपयोग करके, हम इसके तर्क, या तो इसकी स्थिति, इसका मान या संख्या की श्रेणी का उल्लेख करके तत्वों को सेट से हटा सकते हैं।

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

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

उदाहरण कोड

#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;
}

आउटपुट

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
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: 2
Enter value to be inserted: 1
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: 2
Enter value to be inserted: 2
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: 2
Enter value to be inserted: 3
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: 2
Enter value to be inserted: 4
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: 4
Displaying Set by Iterator: 1 2 3 4
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: 3
Enter the element to be deleted: 2
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: 4
Displaying Set by Iterator: 1 3 4
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: 5

Exit code: 1

  1. सी ++ एसटीएल में बनाम मानचित्र सेट करें

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

  1. सी ++ एसटीएल में निचला_बाउंड () फ़ंक्शन सेट करें

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

  1. सी ++ एसटीएल में ढूंढें () फ़ंक्शन सेट करें

    C++ STL में सेट ढूंढें () फ़ंक्शन सेट कंटेनर में खोजे गए तत्व के लिए एक पुनरावर्तक देता है। इटरेटर सेट में अंतिम तत्व के ठीक बाद की स्थिति को इंगित करता है, यदि तत्व नहीं मिला है। एल्गोरिदम Begin    Define function printS() to print elements of set container.    initialize an emp