ในบทช่วยสอนนี้ เราจะมาสำรวจการแทรก (Insertion) และการลบ (Deletion) ข้อมูลใน STL Set ของภาษา C++ ครับ
Set เป็นโครงสร้างข้อมูลแบบ Container ที่มีคุณสมบัติพิเศษคือ จัดเก็บค่าที่ ไม่ซ้ำกัน (Unique) เท่านั้น และจะจัดเก็บข้อมูลในลำดับที่ เรียงลำดับจากน้อยไปมาก (Sorted) โดยอัตโนมัติ ทำให้เหมาะสำหรับการค้นหาและจัดการข้อมูลที่ต้องการความเป็นเอกลักษณ์และลำดับ
ตัวอย่างการแทรกข้อมูล (Insertion)
มีหลายวิธีในการแทรกข้อมูลเข้า Set ดูจากโค้ดตัวอย่างด้านล่าง:
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> st; // ประกาศ Set ของจำนวนเต็ม
set<int>::iterator it = st.begin(); // Iterator ชี้ต้นทาง
set<int>::iterator it1, it2; // Iterator สำหรับวนลูป
pair< set<int>::iterator, bool> ptr; // Pair สำหรับรับค่าผลลัพธ์จาก insert()
// 1. แทรกค่าเดียว และตรวจสอบว่าแทรกใหม่หรือมีอยู่แล้ว
ptr = st.insert(20);
if (ptr.second)
cout << "The element was newly inserted";
else
cout << "The element was already present";
cout << "\nThe set elements after 1st insertion are : ";
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " ";
// 2. แทรกค่าพร้อมระบุตำแหน่ง hint (it) เพื่อเพิ่มประสิทธิภาพ
st.insert(it, 24);
cout << "\nThe set elements after 2nd insertion are : ";
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " ";
// 3. แทรกช่วงข้อมูลจาก Array (Range Insertion)
int arr[3] = { 25, 24, 26 }; // 24 จะไม่ถูกเพิ่มเพราะซ้ำ
st.insert(arr, arr + 3);
cout << "\nThe set elements after 3rd insertion are : ";
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " ";
return 0;
}
ผลลัพธ์ (Output)
The element was newly inserted The set elements after 1st insertion are : 20 The set elements after 2nd insertion are : 20 24 The set elements after 3rd insertion are : 20 24 25 26
ตัวอย่างการลบข้อมูล (Deletion)
การลบข้อมูลใน Set สามารถทำได้หลายรูปแบบ เช่น ลบโดย Iterator, ลบโดยค่า (Key), หรือลบช่วง (Range)
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> st;
set<int>::iterator it, it1, it2;
pair< set<int>::iterator, bool> ptr;
// เตรียมข้อมูล: 5, 10, 15, 20, 25, 30, 35, 40, 45
for (int i = 1; i < 10; i++)
st.insert(i * 5);
cout << "The set elements after insertion are : ";
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " ";
cout << endl;
// 1. ลบโดย Iterator (ลบตัวที่ 2 คือ 10)
it = st.begin();
++it; // ชี้ไปที่ 10
st.erase(it); // ลบ 10 ออก
cout << "The set elements after 1st deletion are : ";
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " ";
// 2. ลบโดย Key (ลบค่า 40)
st.erase(40);
cout << "\nThe set elements after 2nd deletion are : ";
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " ";
// 3. ลบช่วง (Range Erase) ลบตั้งแต่ตำแหน่งที่ 5 จนจบ (15, 20, 25, 30, 35, 45)
it = st.begin();
advance(it, 4); // ขยับ iterator 4 ครั้ง (ชี้ที่ 25)
st.erase(it, st.end());
cout << "\nThe set elements after 3rd deletion are : ";
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " ";
cout << endl;
return 0;
}
ผลลัพธ์ (Output)
The set elements after insertion are : 5 10 15 20 25 30 35 40 45 The set elements after 1st deletion are : 5 15 20 25 30 35 40 45 The set elements after 2nd deletion are : 5 15 20 25 30 35 45 The set elements after 3rd deletion are : 5 15 20
สรุปฟังก์ชันสำคัญของ Set
insert(val): แทรกค่า คืนค่า pair<iterator, bool> บอกว่าสำเร็จหรือซ้ำinsert(pos, val): แทรกค่าพร้อม hint ตำแหน่ง (เพิ่มประสิทธิภาพ)insert(first, last): แทรกช่วงข้อมูลจาก Iterator rangeerase(pos): ลบข้อมูลที่ Iterator ชี้อยู่erase(key): ลบข้อมูลตามค่า Key คืนจำนวนที่ลบได้ (0 หรือ 1)erase(first, last): ลบช่วงข้อมูลระหว่าง Iterator
Set ใช้งานง่าย เหมาะสำหรับโจทย์ที่ต้องการข้อมูลไม่ซ้ำและเรียงลำดับอัตโนมัติครับ