Set & map

簡介

set

自動排序且元素不重複的容器

  • 元素插入時會自動從小到大排好
  • 重複的元素無法被插入
  • 元素一經插入就無法直接修改,若想修改必須先刪除再重新插入

map

自動排序的鍵值對(Key-Value Pair)容器

  • 由 Key 決定排序(預設從小到大
  • Key 絕對不能重複,Value 則沒有限制
  • Key 被保護無法修改,但可以透過迭代器或中括號直接修改對應的 Value
{1, 8, 10, 17, 29}
{{30, aaa}, {50, bbb}, {90, ccc}}

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

#include <set>

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 - 函式

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⁠ 的元素

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 碼順序排列

#include <map>

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型態, greater<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[key]

  • 修改已存在的 Key 
  • 插入不存在的 Key
map<string, int> m = {
    {"Math", 95},
    {"Physics", 88},
};
m["Math"] = 100;  // Math 的 value 改成 100
m["English"] = 85;  // 插入新科目 {"English", 85}
m["Chinese"];  // 插入新的 key,value 預設為0 ({"Chinese", 0})
{"Chinese", 0}
{"English", 85}
{"Math", 100}
{"Physics", 88}

Map - 函式

m.insert(k, v)

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

Map - 函式

first / second

  •  it.first  代表 Key  
  •  it.second 代表 Value
map<string,int> m;
m={
    {"zsisc",32},
    {"cmioc",32},
    {"ckcsc",38},
    {"infor",38}
};
for (auto it : m) {
        cout << it.first << " " << it.second << endl;
}
ckcsc 38 
cmioc 32 
infor 38 
zsisc 32 

Map - 函式

first / second

  •  it->first  代表 Key   (唯讀,不可修改)
  •  it->second 代表 Value (可讀寫,可修改)
map<string, int> m = {
    {"Physics", 90},
    {"Chemistry", 85}
};

auto it = m.find("Physics");
 
cout << it->first << "\n";   // 答案:Physics
cout << it->second << "\n";  // 答案:90

it->first = "Biology";    // 錯誤!Key 被 const 保護,強行修改會編譯失敗
it->second = 95;          // 成功!Physics 的分數被改成 95

Reviewwwww

By ariel tai

Reviewwwww

  • 57