ที่นี่เราจะดูความแตกต่างของ set, multiset, unordered_set และ unordered_multiset ใน C++ มาดูคุณสมบัติของแต่ละตัวด้วยตัวอย่าง
Set
คุณสมบัติของ set มีดังนี้
- เก็บข้อมูลเรียงลำดับ (Sorted Order)
- เก็บค่าที่ไม่ซ้ำกัน (Unique Values) เท่านั้น
- สามารถแทรก (Insert) หรือลบ (Delete) ข้อมูลได้ แต่ไม่สามารถเปลี่ยนข้อมูลได้
- สามารถลบมากกว่าหนึ่งองค์ประกอบด้วย Start และ End Iterator
- สามารถ Traverse ด้วย Iterators
- Sets ถูก Implement ด้วย Binary Search Tree
มาดูตัวอย่าง
ตัวอย่าง
#include <iostream> #include <set> using namespace std; main() { int data[15] = {11, 55, 22, 66, 33, 22, 11, 44, 77, 88, 66, 99, 66, 23, 41}; set<int> my_set; for(int i = 0; i<15; i++) { my_set.insert(data[i]); } set<int>::iterator it; for(it = my_set.begin(); it != my_set.end(); it++) { cout << "Item: " << *it << endl; } }
ผลลัพธ์
Item: 11 Item: 22 Item: 23 Item: 33 Item: 41 Item: 44 Item: 55 Item: 66 Item: 77 Item: 88 Item: 99
Multiset
คุณสมบัติของ multiset มีดังนี้ -
- เก็บข้อมูลเรียงลำดับ (Sorted Order)
- อนุญาตให้เก็บข้อมูลซ้ำ (Duplicate Data) ได้
- สามารถลบมากกว่าหนึ่งองค์ประกอบด้วย Start และ End Iterator
มาดูตัวอย่าง
ตัวอย่าง
#include <iostream> #include <set> using namespace std; main() { int data[15] = {11, 55, 22, 66, 33, 22, 11, 44, 77, 88, 66, 99, 66, 23, 41}; multiset<int> my_set; for(int i = 0; i<15; i++) { my_set.insert(data[i]); } multiset<int>::iterator it; for(it = my_set.begin(); it != my_set.end(); it++) { cout << "Item: " << *it << endl; } }
ผลลัพธ์
Item: 11 Item: 11 Item: 22 Item: 22 Item: 23 Item: 33 Item: 41 Item: 44 Item: 55 Item: 66 Item: 66 Item: 66 Item: 77 Item: 88 Item: 99
Unordered Set
คุณสมบัติของ unordered_set มีดังนี้ -
- ข้อมูลสามารถวางได้ในลำดับใดๆ (Any Order)
- ข้อมูลซ้ำจะถูกทิ้ง (Discarded)
- Set นี้สร้างด้วย Hash Tables
- สามารถ Erase ได้เพียงหนึ่งองค์ประกอบที่ Iterator ชี้อยู่
มาดูตัวอย่าง
ตัวอย่าง
#include <iostream> #include <unordered_set> using namespace std; main() { int data[15] = {11, 55, 22, 66, 33, 22, 11, 44, 77, 88, 66, 99, 66, 23, 41}; unordered_set<int> my_set; for(int i = 0; i<15; i++) { my_set.insert(data[i]); } unordered_set<int>::iterator it; for(it = my_set.begin(); it != my_set.end(); it++) { cout << "Item: " << *it << endl; } }
ผลลัพธ์
Item: 11 Item: 55 Item: 22 Item: 66 Item: 33 Item: 44 Item: 77 Item: 88 Item: 99 Item: 23 Item: 41
Unordered Multiset
คุณสมบัติของ unordered_multiset มีดังนี้ -
- ข้อมูลสามารถวางได้ในลำดับใดๆ (Any Order)
- อนุญาตข้อมูลซ้ำ (Duplicate Data Allowed)
- Set นี้สร้างด้วย Hash Tables
- สามารถ Erase ได้เพียงหนึ่งองค์ประกอบที่ Iterator ชี้อยู่
มาดูตัวอย่าง
ตัวอย่าง
#include <iostream> #include <unordered_set> using namespace std; main() { int data[15] = {11, 55, 22, 66, 33, 22, 11, 44, 77, 88, 66, 99, 66, 23, 41}; unordered_multiset<int> my_set; for(int i = 0; i<15; i++) { my_set.insert(data[i]); } unordered_multiset<int>::iterator it; for(it = my_set.begin(); it != my_set.end(); it++) { cout << "Item: " << *it << endl; } }
ผลลัพธ์
Item: 11 Item: 55 Item: 22 Item: 66 Item: 33 Item: 22 Item: 11 Item: 44 Item: 77 Item: 88 Item: 66 Item: 99 Item: 66 Item: 23 Item: 41