Insertion (การแทรก)
Insertion ใน STL set สามารถทำได้โดย insert() และ emplace() operation
Insert(): Insert() ใช้เพื่อแทรก elements ลงใน set Insert operation รับ reference ไปยัง object
List of functions are used:
- st.size() = Returns the size of set.
- st.insert() = It is used to insert elements to the set.
ตัวอย่างโค้ด
#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 ... 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
Emplace()
Emplace operation ใช้แทรก elements ลงใน set แบบ in-place มันหลีกเลี่ยงการ copy object ที่ไม่จำเป็น และทำ insertion ได้มีประสิทธิภาพมากกว่า insert operation
List of functions used:
- st.size() = Returns the size of set.
- st.emplace() = It is used to insert elements to the set.
ตัวอย่างโค้ด
#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 ... 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
Deletion (การลบ)
ใช้ erase() function เราสามารถลบ elements จาก set ได้โดยระบุ argument หรือเป็นตำแหน่ง ค่า หรือช่วงของตัวเลข
List of functions used here:
- st.size() = Returns the size of set.
- st.insert() = It is used to insert elements to the set.
- st.erase() = To delete the element from the set
ตัวอย่างโค้ด
#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 ... 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