By 睡眠不足ㄉ淮哥
我的標題完全沒意義
而且他們是不是在排擠我
一定是!!!
今天這裡放的是
十分可愛而且還有耳機的北門
設定 集合
當然要用之前要 #include<set>
set<型態> 命名
#define pii pair<int,int>
set<int> num;
set<pii> score;
預設從小排到大
把值插入set中
如果set中已有相同元素則 什麼都不會發生 就像鯉魚王的躍起 哈哈
set<int> num;
num.insert(3);//{3}
num.insert(5);//{3,5}
num.insert(3);//{3,5}
回傳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
踢除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!!!";
}
回傳set剩餘的元素數量
set<int> num;
num.insert(3);//{3}
num.insert(5);//{3,5}
num.insert(3);//{3,5}
cout << num.size();//輸出2
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是否為空
強制清空set中所有元素
set<int> num;
num.insert(3);//{3}
num.insert(5);//{3,5}
num.insert(3);//{3,5}
if(!num.empty()){
num.clear();
}
//{}
顧名思義,未經排序的set
語法與set基本一致
只是將宣告的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
顧名思義,有重複元素的set
語法與set基本一致
只是將宣告的set改成multiset
multiset<int> num;
num.insert(1);//{1}
num.insert(1);//{1,1}
num.clear();
if(num.empty()){
cout << "nothing";
}
else{
cout << "something";
}
//輸出nothing
地圖 關聯式容器
當然要用之前要 #include<map>
啊它就是
index可以不為數字的
array啊
map<key型態,value型態> 命名
map <int, int> num; // int 對到 int
map <char, int> a; // char 對到 int
map <string, int> animal; // string 對到 int
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["佑佑"];
//沒關係我先逝世
啊就,清空
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是好東西
顧名思義,未經排序的map
語法與map基本一致
只是將宣告的map改成unordered_map
好眼熟的前綴
嘿嘿 你們知道把一鍋熱水煮沸
掰掰掰掰~~~~