Set & map

戴瑢溱

Index

簡介

Set

set - 宣告

set<資料型態> 容器名稱;
set<資料型態> 容器名稱 = {};
set<int> s1;          // 裝整數的 set (由小到大)
set<string> s2;       // 裝字串的 set(依字典序由小到大)

set<int> s3 = {30, 10, 50, 20, 40}; // 10, 20, 30, 40, 50
set<int> s4 = {30, 10, 20, 20, 30}; // 10, 20, 30

set - 宣告

// 由小到大 (與預設相同)
set<int, less<int>> s6; 
s6.insert(10);
s6.insert(30);
s6.insert(20);  
// 10, 20, 30
set<資料型態, greater<資料型態>> 容器名稱;
set<資料型態, less<資料型態>> 容器名稱;
// 由大到小
set<int, greater<int>> s5; 
s5.insert(10);
s5.insert(30);
s5.insert(20);  
// 30, 20, 10

set - for 迴圈

set<int> s = {30, 10, 20};

for (auto x : s) {
    cout << x << " "; 
}
// 輸出:10 20 30
for(資料型態(常用auto) 變數 : 容器)

利用 for 迴圈遍歷 set 中的元素 (由小到大)

Set - 函式

#include <set>

s.insert(x)插入元素 ⁠x⁠s.begin()指向 第一個元素(最小值)的位置
s.erase(x)刪除值為 ⁠x⁠ 的元素s.rbegin()指向 最後一個元素(最大值)的位置
s.size()回傳元素總數s.end()指向 最後一個元素的下一個
s.empty ()檢查 ⁠set⁠ 是否為空s.count(x)計算值為 ⁠x⁠ 的數量
s.clear()清空 ⁠set⁠s.find(x)尋找值為 ⁠x⁠ 的元素

Set - 函式

s.insert(x)

  • x 不存在 : 把 x 放進正確的位置
  • x 已存在 : 直接忽略這次的插入,容器不會有任何改變
set<int> s;
s.insert(10); // 插入 10
s.insert(30); // 插入 30,自動排在 10 後面
s.insert(20); // 插入 20,自動插在 10 和 30 中間

s.insert(10); // 重複了!set 的大小依然是 3,內部只有 {10, 20, 30}

Set - 函式

s.erase(x)

  • 直接指定想刪除的元素值
  • 回傳值 : 回傳成功刪除的數量.                                                                   因為 set 的元素唯一, 所以只會回傳 1(成功刪除)或 0(不存在).
set<int> s = {10, 20, 30, 40};
s.erase(30); // 刪除30
int num1 = s.erase(20); // 刪除 20,num1 的值為 1
int num2 = s.erase(50); // 找不到 50,num2 的值為 0

Set - 函式

s.size()

回傳元素總數

set<int> s = {1,2,3};
cout << s.size();  // 3

s.insert(4); 
s.insert(5); 
s.insert(6); 

cout << s.size();  // 6

Set - 函式

s.empty()

檢查容器是否為空

set<int> s = {10};
cout << s.empty();  // 0

s.erase(10); 
cout << s.empty(); // 1

Set - 函式

s.clear()

清空 set 中的元素

set<int> s = {10, 20, 30, 40, 50};
cout << s.size() << endl;  // 5
cout <<  s.empty() << endl; // 0

s.clear();

cout << s.size() << endl;  // 0
cout <<  s.empty() << endl; // 1

Set - 函式

s.begin() / s.rbegin() / s.end()

 元素內容:   [ 10 ]     ->     [ 20 ]     ->     [ 30 ]     ->     [ 虛擬結尾 ]
                         ↑                                                                        ↑
                   s.begin()                                  s.rbegin()              s.end()
                   (最小值 )                                   (最大值)     (不指向任何元素)

set<int> s = {30, 10, 20};

cout << *s.begin();  // 10
cout << *s.rbegin(); // 30
for (auto it = s.begin(); it != s.end(); ++it) {
	cout << *it;  // 10, 20, 30
}
int bug = *s.end(); //不可以!記憶體區段錯誤

Set - 函式

s.count()

計算容器中值為 x 的數量

因為 set 的元素唯一, 所以只會回傳 1(成功刪除)或 0(不存在).

set<int> s = {10, 20, 30};

int c1 = s.count(20); // 找到了!回傳 1
int c2 = s.count(50); // 找不到!回傳 0

// 檢查是否存在
if (s.count(20)) {
    // 存在時要做的操作 (因為 1 代表 true)
}
if (!s.count(50)) {
    // 不存在時要做的操作 (因為 0 代表 false)
}

Set - 函式

s.find()

尋找值為 x 的元素

  • x 存在 : 回傳 x 的位置
  • x 不存在 :  回傳s.end()
set<int> s = {10, 20, 30};
// 成功找到
auto it = s.find(20); 
if (it != s.end()) {
    int val = *it; // 20
}
// 找不到
auto missing_it = s.find(50);
if (missing_it == s.end()) {
    // 找不到!當 set 找不到元素時,會直接回傳 s.end()(虛擬結尾)
}
// 配合 erase() 刪除
auto delete_it = s.find(20);
if (delete_it != s.end()) {
    s.erase(delete_it); // 如果 20 存在就刪除
}

Map

Map - 宣告

map<Key型態, Value型態> 容器名稱;
map<key型態, value型態> 容器名稱 = {{k1, v1}, {k2, v2}};
map<string, int> m1;
map<string, int> m2 = {
    {"infor", 38},
    {"zsisc", 32},
    {"ckcsc", 38},
    {"cmioc", 32}
};
// 自動排序後的順序:
{"ckcsc", 38},  // ck
{"cmioc", 32},  // cm
{"infor", 38},  // i
{"zsisc", 32}   // z

依照 ASCII 碼順序排列

Map - 宣告

// 由小到大 (與預設相同,只針對 Key 排序)
map<int, string, less<int>> m3;
m3.insert({10, "wawawa"});
m3.insert({30, "cactus"});
m3.insert({20, "shiny"});
// Key 排序為:10, 20, 30
set<key型態, value型態, less<key型態>> 容器名稱;
set<key型態, value型態, less<key型態>> 容器名稱;
// 由大到小 (只針對 Key 排序)
map<int, string, greater<int>> m4;
m4.insert({10, "wawawa"});
m4.insert({30, "cactus"});
m4.insert({20, "shiny"});
// Key 排序為:30, 20, 10

Map - 函式

m.insert(x, y)

  • x 不存在 : 把 x 放進正確的位置
  • x 已存在 : 直接忽略這次的插入,容器不會有任何改變
map<string, int> m = {
    {"Math", 95},
    {"Physics", 88},
};
m.insert({"Chemistry", 92});  // 成功插入
m.insert({"Math", 74});  
// 原本就有 Math 了,不執行,Math 的  value 依舊是95

kahoot !

Copy of DP-2

By ariel tai

Copy of DP-2

  • 27