Set
Sets are containers that store unique elements following a specific order.
In a set, the value of an element also identifies it (the value is itself the key, of type T), and each value must be unique. The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.
Internally, the elements in a set are always sorted following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).
Set containers are generally slower than unordered_set containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order.
Program for set implementation in C++ STL
#include <iostream> #include <set> #include <string> #include <cstdlib> using namespace std; int main() { set<int> st; set<int>::iterator it; int choice, item; while (1) { cout<<"n---------------------"<<endl; cout<<"Set Implementation in C++ Stl"<<endl; cout<<"n---------------------"<<endl; cout<<"1.Insert Element into the Set"<<endl; cout<<"2.Delete Element of the Set"<<endl; cout<<"3.Size of the Set"<<endl; cout<<"4.Find Element in a Set"<<endl; cout<<"5.Dislplay by Iterator"<<endl; cout<<"6.Exit"<<endl; cout<<"Enter your Choice: "; cin>>choice; switch(choice) { case 1: cout<<"Enter value to be inserted: "; cin>>item; st.insert(item); break; case 2: cout<<"Enter the element to be deleted: "; cin>>item; st.erase(item); break; case 3: cout<<"Size of the Set: "; cout<<st.size()<<endl; break; case 4: cout<<"Enter the element to be found: "; cin>>item; it = st.find(item); if (it != st.end()) cout<<"Element "<<*it<<" found in the set" <<endl; else cout<<"No Element Found"<<endl; break; case 5: cout<<"Displaying Map by Iterator: "; for (it = st.begin(); it != st.end(); it++) { cout << (*it)<<" "; } cout<<endl; break; case 6: exit(1); break; default: cout<<"Wrong Choice"<<endl; } } return 0; }
Sample Output:
--------------------- Set Implementation in C++ Stl --------------------- 1.Insert Element into the Set 2.Delete Element of the Set 3.Size of the Set 4.Find Element in a Set 5.Dislplay by Iterator 6.Exit Enter your Choice: 1 Enter value to be inserted: 1 --------------------- Set Implementation in C++ Stl --------------------- 1.Insert Element into the Set 2.Delete Element of the Set 3.Size of the Set 4.Find Element in a Set 5.Dislplay by Iterator 6.Exit Enter your Choice: 1 Enter value to be inserted: 2 --------------------- Set Implementation in C++ Stl --------------------- 1.Insert Element into the Set 2.Delete Element of the Set 3.Size of the Set 4.Find Element in a Set 5.Dislplay by Iterator 6.Exit Enter your Choice: 1 Enter value to be inserted: 3 --------------------- Set Implementation in C++ Stl --------------------- 1.Insert Element into the Set 2.Delete Element of the Set 3.Size of the Set 4.Find Element in a Set 5.Dislplay by Iterator 6.Exit Enter your Choice: 1 Enter value to be inserted: 4 --------------------- Set Implementation in C++ Stl --------------------- 1.Insert Element into the Set 2.Delete Element of the Set 3.Size of the Set 4.Find Element in a Set 5.Dislplay by Iterator 6.Exit Enter your Choice: 1 Enter value to be inserted: 5 --------------------- Set Implementation in C++ Stl --------------------- 1.Insert Element into the Set 2.Delete Element of the Set 3.Size of the Set 4.Find Element in a Set 5.Dislplay by Iterator 6.Exit Enter your Choice: 1 Enter value to be inserted: 4 --------------------- Set Implementation in C++ Stl --------------------- 1.Insert Element into the Set 2.Delete Element of the Set 3.Size of the Set 4.Find Element in a Set 5.Dislplay by Iterator 6.Exit Enter your Choice: 1 Enter value to be inserted: 3 --------------------- Set Implementation in C++ Stl --------------------- 1.Insert Element into the Set 2.Delete Element of the Set 3.Size of the Set 4.Find Element in a Set 5.Dislplay by Iterator 6.Exit Enter your Choice: 1 Enter value to be inserted: 2 --------------------- Set Implementation in C++ Stl --------------------- 1.Insert Element into the Set 2.Delete Element of the Set 3.Size of the Set 4.Find Element in a Set 5.Dislplay by Iterator 6.Exit Enter your Choice: 1 Enter value to be inserted: 1 --------------------- Set Implementation in C++ Stl --------------------- 1.Insert Element into the Set 2.Delete Element of the Set 3.Size of the Set 4.Find Element in a Set 5.Dislplay by Iterator 6.Exit Enter your Choice: 3 Size of the Set: 5 --------------------- Set Implementation in C++ Stl --------------------- 1.Insert Element into the Set 2.Delete Element of the Set 3.Size of the Set 4.Find Element in a Set 5.Dislplay by Iterator 6.Exit Enter your Choice: 5 Displaying Map by Iterator: 1 2 3 4 5 --------------------- Set Implementation in C++ Stl --------------------- 1.Insert Element into the Set 2.Delete Element of the Set 3.Size of the Set 4.Find Element in a Set 5.Dislplay by Iterator 6.Exit Enter your Choice: 4 Enter the element to be found: 3 Element 3 found in the set --------------------- Set Implementation in C++ Stl --------------------- 1.Insert Element into the Set 2.Delete Element of the Set 3.Size of the Set 4.Find Element in a Set 5.Dislplay by Iterator 6.Exit Enter your Choice: 2 Enter the element to be deleted: 5 --------------------- Set Implementation in C++ Stl --------------------- 1.Insert Element into the Set 2.Delete Element of the Set 3.Size of the Set 4.Find Element in a Set 5.Dislplay by Iterator 6.Exit Enter your Choice: 3 Size of the Set: 4 --------------------- Set Implementation in C++ Stl --------------------- 1.Insert Element into the Set 2.Delete Element of the Set 3.Size of the Set 4.Find Element in a Set 5.Dislplay by Iterator 6.Exit Enter your Choice: 5 Displaying Map by Iterator: 1 2 3 4 --------------------- Set Implementation in C++ Stl --------------------- 1.Insert Element into the Set 2.Delete Element of the Set 3.Size of the Set 4.Find Element in a Set 5.Dislplay by Iterator 6.Exit Enter your Choice: 6 ------------------