ในบทความนี้เราจะอธิบายการทำงาน ไวยากรณ์ และตัวอย่างของฟังก์ชัน list::splice() ใน C++ Standard Template Library (STL)
List ใน STL คืออะไร?
List เป็นโครงสร้างข้อมูลที่รองรับการแทรกและลบข้อมูลได้ในเวลาคงที่ (constant time) ที่ตำแหน่งใดก็ได้ในลำดับ List ถูกนำไปใช้งานเป็น doubly linked list (รายการเชื่อมโยงสองทิศทาง) ทำให้จัดสรรหน่วยความจำแบบไม่ต่อเนื่องได้ List ให้ประสิทธิภาพการแทรก ดึง และย้ายข้อมูลที่ตำแหน่งใดก็ได้ในคอนเทนเนอร์ดีกว่า array, vector และ deque อย่างไรก็ตาม การเข้าถึงข้อมูลโดยตรง (random access) ใน List ช้ากว่า และ List คล้ายกับ forward_list แต่ forward_list เป็น singly linked list (รายการเชื่อมโยงทิศทางเดียว) จึงวนลูปได้เฉพาะทิศทางไปข้างหน้าเท่านั้น
list::splice() คืออะไร?
list::splice() เป็นฟังก์ชันในตัวของ C++ STL ที่ประกาศใน header file <list> ฟังก์ชันนี้ใช้สำหรับ ย้าย (transfer) องค์ประกอบ จาก list container หนึ่งไปยังอีก list container หนึ่งที่ตำแหน่งที่กำหนด ฟังก์ชันนี้จะแทรกเนื้อหาลงใน list ปลายทาง และนำองค์ประกอบเหล่านั้นออกจาก list ต้นทาง ทำให้ขนาดของทั้งสอง list เปลี่ยนไป
ไวยากรณ์ (Syntax)
ฟังก์ชันนี้มี 3 รูปแบบการใช้งาน:
// 1. ย้ายทั้ง list2 ไปไว้ที่ position ใน list1 list1.splice(position, list2); // 2. ย้ายองค์ประกอบเดียวที่ iterator i ชี้อยู่ใน list2 ไปไว้ที่ position ใน list1 list1.splice(position, list2, i); // 3. ย้ายช่วงองค์ประกอบจาก first ถึง last ใน list2 ไปไว้ที่ position ใน list1 list1.splice(position, list2, first, last);
พารามิเตอร์ (Parameters)
- position – ตำแหน่งใน list1 ที่ต้องการให้องค์ประกอบถูกย้ายไปวาง
- list2 – list ต้นทางที่ต้องการย้ายองค์ประกอบออก
- i – iterator ที่ระบุตำแหน่งใน list2 ขององค์ประกอบที่ต้องการย้าย (ย้ายเพียงองค์ประกอบเดียว)
- first, last – iterator คู่ที่กำหนดช่วง [first, last) ใน list2 ที่ต้องการย้าย
ตัวอย่างการใช้งาน
Input: list<int> List_container = {10, 11, 13, 15};
list<int> List2 = {1, 2, 3, 4};
List_container.splice(2, List2); // สมมติ position คือ iterator ชี้ไปที่ตำแหน่งที่ 2
Output: List_container = 10 11 1 2 3 4 13 15
List2 = (ว่างเปล่า)
ค่าที่ส่งคืน (Return Value)
ฟังก์ชันนี้ไม่ส่งคืนค่าใดๆ (void) หมายเหตุ: บทความต้นฉบับระบุว่าส่งคืน reverse iterator ซึ่งอาจเป็นข้อมูลผิดพลาด เนื่องจาก splice() ในมาตรฐาน C++ ไม่มีค่าส่งคืน
ตัวอย่างโค้ดเต็ม 1
#include <bits/stdc++.h>
using namespace std;
int main() {
list<int> myList_1 = { 10, 20, 30, 40 };
list<int> myList_2 = { 50, 60 };
list<int>::iterator i = myList_2.begin();
myList_1.splice(myList_1.end(), myList_2, i);
cout << "list after splice operation" << endl;
for (auto temp : myList_1)
cout << temp << " ";
return 0;
}
ผลลัพธ์ (Output)
list after splice operation 10 20 30 40 50
ตัวอย่างโค้ดเต็ม 2
#include <bits/stdc++.h>
using namespace std;
int main() {
list<int> myList_1 = { 10, 20 };
list<int> myList_2 = { 30, 40, 50 };
list<int> myList_3 = { 60 };
myList_1.splice(myList_1.begin(), myList_2);
cout << "list 1 after splice operation" << endl;
for (auto x : myList_1) cout << x << " ";
myList_3.splice(myList_3.end(), myList_1);
cout << "\nlist 3 after splice operation" << endl;
for (auto x : myList_3) cout << x << " ";
return 0;
}
ผลลัพธ์ (Output)
list 1 after splice operation 30 40 50 10 20 list 3 after splice operation 60 30 40 50 10 20