戴瑢溱
set
自動排序且元素不重複的容器
map
自動排序的鍵值對(Key-Value Pair)容器
{1, 8, 10, 17, 29}{{30, aaa}, {50, bbb}, {90, ccc}}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<int, less<int>> s6;
s6.insert(10);
s6.insert(30);
s6.insert(20);
// 10, 20, 30set<資料型態, greater<資料型態>> 容器名稱;set<資料型態, less<資料型態>> 容器名稱;// 由大到小
set<int, greater<int>> s5;
s5.insert(10);
s5.insert(30);
s5.insert(20);
// 30, 20, 10set<int> s = {30, 10, 20};
for (auto x : s) {
cout << x << " ";
}
// 輸出:10 20 30for(資料型態(常用auto) 變數 : 容器)利用 for 迴圈遍歷 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 的元素 |
s.insert(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}s.erase(x)
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 的值為 0s.size()
回傳元素總數
set<int> s = {1,2,3};
cout << s.size(); // 3
s.insert(4);
s.insert(5);
s.insert(6);
cout << s.size(); // 6s.empty()
檢查容器是否為空
set<int> s = {10};
cout << s.empty(); // 0
s.erase(10);
cout << s.empty(); // 1s.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; // 1s.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(); //不可以!記憶體區段錯誤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)
}s.find()
尋找值為 x 的元素
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 存在就刪除
}set<int> s;
s.insert(30);
s.insert(10);
s.insert(20);
s.insert(10);
cout << s.size() << endl;
cout << *s.begin() << endl;
cout << s.count(10) << endl;
for (auto it : s) {
cout << it << " ";
}3
10
1
10 20 30map<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>
// 由小到大 (與預設相同,只針對 Key 排序)
map<int, string, less<int>> m3;
m3.insert({10, "wawawa"});
m3.insert({30, "cactus"});
m3.insert({20, "shiny"});
// Key 排序為:10, 20, 30set<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, 10m.insert(k, v)
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}m[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}first / second
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 first / second
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 的分數被改成 95map<string, int> m;
m.insert({"APCS", 4});
m["C++"] = 3;
m.insert({"APCS", 5});
m["Python"];
cout << m.size() << endl;
cout << m["APCS"] << endl;
cout << m["Python"] << endl;
if (m.find("Java") != m.end()) {
cout << "1" << endl;
} else {
cout << "0" << endl;
}3
4
0
0map<string, int> m = {
{"ZJ", 10},
{"APCS", 20}
};
auto it = m.find("APCS");
if (it != m.end()) {
it->second = 30;
}
m.insert({"ZJ", 50});
for (auto it : m) {
cout << it.first << " " << it.second << endl;
}APCS 30
ZJ 10