bigshoot[-1]
By 睡眠不足ㄉ淮哥
我的標題完全沒意義
而且他們是不是在排擠我
一定是!!!
今天要教什麼
- set
- map
- nothing
- another nothing
- still another nothing
- the other nothing
今天這裡放的是
十分可愛而且還有耳機的北門
set
設定 集合
set是什麼
- 就是數學上的集合
- 具有唯一性,每個元素只有一個
- 內容會自動排序
- 本身是一個紅黑樹
- 不能用 [ ]、.top() 等,只能用迭代器取值
- 好多衍生東東
當然要用之前要 #include<set>
宣告
set<型態> 命名
#define pii pair<int,int>
set<int> num;
set<pii> score;
預設從小排到大
.insert()
把值插入set中
如果set中已有相同元素則 什麼都不會發生 就像鯉魚王的躍起 哈哈
set<int> num;
num.insert(3);//{3}
num.insert(5);//{3,5}
num.insert(3);//{3,5}
.count()
回傳set中某個元素的數量
因為在set中數量非0即1,所以可以拿來當bool用
set<int> num;
num.insert(3);//{3}
num.insert(5);//{3,5}
num.insert(3);//{3,5}
num.count(1);//回傳0
num.count(3);//回傳1
.erase()
踢除set中某個元素,並回傳true
若沒有該元素可踢除,則回傳false
set<int> num;
num.insert(3);//{3}
num.insert(5);//{3,5}
num.insert(3);//{3,5}
if(num.erase(3)){
cout << "byebye 3";
}
else{
cout << "3 does not exist!!!";
}
.size()
回傳set剩餘的元素數量
set<int> num;
num.insert(3);//{3}
num.insert(5);//{3,5}
num.insert(3);//{3,5}
cout << num.size();//輸出2
.empty()
set<int> num;
num.insert(3);//{3}
num.insert(5);//{3,5}
num.insert(3);//{3,5}
if(!num.empty()){
cout << "something in the set\n"
num.erase(3);
num.erase(5);
}
else{
cout << "now nothing in the set\n"
}
//輸出
//something in the set
//now nothing in the set
回傳一個bool,代表set是否為空
.clear()
強制清空set中所有元素
set<int> num;
num.insert(3);//{3}
num.insert(5);//{3,5}
num.insert(3);//{3,5}
if(!num.empty()){
num.clear();
}
//{}
unordered_set
顧名思義,未經排序的set
語法與set基本一致
只是將宣告的set改成unordered_set
unordered_set
unordered_set<int> num;
num.insert(10);{10}
num.insert(2);{10,2}
num.clear();
if(num.empty()){
cout << "nothing";
}
else{
cout << "something";
}
//輸出nothing
multiset
顧名思義,有重複元素的set
語法與set基本一致
只是將宣告的set改成multiset
multiset
multiset<int> num;
num.insert(1);//{1}
num.insert(1);//{1,1}
num.clear();
if(num.empty()){
cout << "nothing";
}
else{
cout << "something";
}
//輸出nothing
map
地圖 關聯式容器
map是什麼
- 它看起來好陣列喔
- 對阿他基本就是陣列 只是 index 不一定是數字
- 他也會自動排序
- 本身也是紅黑樹
- 沒那麼多衍生東東
當然要用之前要 #include<map>
map是什麼
- 自訂index(key)
- key不侷限於數字
- key有唯一性
- 一個key對一個value
- 也會自動排序
啊它就是
index可以不為數字的
array啊
宣告
map<key型態,value型態> 命名
map <int, int> num; // int 對到 int
map <char, int> a; // char 對到 int
map <string, int> animal; // string 對到 int
加入一個新的key
map <string, string> couple;
couple.insert({"晴", "佑佑"});
你可以
但是insert是壞東西
更簡單的做法是跟array一樣
map <string, string> couple;
couple["晴"] = "佑佑";
取值
啊就,取值,你在 array 中怎麼做,在 map 中就怎麼做
map <string, string> museum;
museum["晴"] = "顯然我是清純小女僕";
museum["世宗"] = "我超勝利";
museum["佑佑"] = "沒關係我先逝世";
museum["Cc"] = "HHPY";
museum["水獺"] = "硬起來了";
museum["柴柴"] = "這就是標準的斷章取義";
museum["807"] = "你檢查腋下";
cout << museum["佑佑"];
//沒關係我先逝世
.clear()
啊就,清空
map <int, int> dic;
dic[71] = 1e9 + 17;
cout << dic[71] << '\n'; //1000000017
dic.clear();
cout << dic[71] << '\n'; //沒看過的 key 預設是 0
小問題
key一樣從0開始往後加,並且可以隨時新增value
學長你有什麼問題幹嘛不用vector就好
小問題
學長你有什麼問題幹嘛不用vector就好
事實上有時候map會優於vector
小問題
假如今天題目給n
$$1\,\le\,n\,\le\,1e15$$
用vector開1e15的大小?
放心時間空間都爛給你看
小問題
假如今天題目給n
$$1\,\le\,n\,\le\,1e15$$
但你發現這麼大的範圍總共只會給你10筆資料
這時候map只要開10格就解決了
所以你會發現
當數據離散時
map是好東西
unordered_map
顧名思義,未經排序的map
語法與map基本一致
只是將宣告的map改成unordered_map
好眼熟的前綴
結束了?
-
嘿嘿 你們知道把一鍋熱水煮沸
- 使其一直震動震動 自瓦斯爐上以初速為V進行平拋運動掉到地上的凹洞裡
- 那鍋水會怎麼樣嗎?
- 會沸到窟
- 對 就跟我一樣
- 哈哈
結束了。
掰掰掰掰~~~~
Bigshootclass
By willyyoung
Bigshootclass
Big Shoot Class
- 78