Review
Set & Map
目錄
SET
MAP
SET
SET
- #include<set>
SET
- #include<set>
- 預設遞增排序(和插入順序無關)
- 元素不能重複(唯一性)
- 便於快速找尋資料是否已經存在
• 口 •
!
#include<iostream>
#include<set>
using namespace std;
int main()
{
/*宣告
set<資料型別> 名稱 (預設為遞增) */
set<int> set1={1,3,5,2,4,6,6};
/*遞增排序
set<資料型別,less <資料型別>> 名稱*/
set<int,less<int>> set2={1,3,5,2,4,6,6};
/*遞減排序
set<資料型別,greater <資料型別>> 名稱*/
set<int,greater<int>> set3={1,3,5,2,4,6,6};
for(auto it:set1)
{
cout<<it<<endl;
}
}
SET的宣告
1 2 3 4 5 6
1 2 3 4 5 6
6 5 4 3 2 1
• _ •
?
• 口 •
!
• _ •
?
SET的函式之增刪大法
insert(x) | 插入元素 x 到 set ,會檢查是否 x 已經在集合內 |
---|---|
erase(it) | 刪除 set 的迭代器 it 位置的元素。 |
erase(x) | 刪除 set 中數值為 x 的元素,回傳刪除幾個元素,回傳值為 0 或 1 |
clear() | 刪除 set 中所有元素 |
#include<iostream>
#include<set>
using namespace std;
int main()
{
set<int> set1={1,3,5,2,4,6};
set1.insert(7);
}
#include<iostream>
#include<set>
using namespace std;
int main()
{
set<int> set1={1,3,5,2,4,6};
set1.clear();
if(set1.empty())
cout<<"empty"<<endl;
}
#include<iostream>
#include<set>
using namespace std;
int main()
{
set<int> set1={1,3,5,2,4,6};
cout<<set1.size()<<endl;
}
1 2 3 4 5 6 7
#include<iostream>
#include<set>
using namespace std;
int main()
{
set<int> set1={1,3,5,2,4,6};
set1.erase(2);
}
#include<iostream>
#include<set>
using namespace std;
int main()
{
set<int> set1={1,3,5,2,4,6};
set1.erase(set1.begin());
}
2 3 4 5 6
1 3 4 5 6
6
empty
• 口 •
!
• _ •
?
SET的函式之讓我找找
#include<iostream>
#include<set>
using namespace std;
int main()
{
set<int> set1={1,3,5,2,4,6};
if(set1.find(2)!=set1.end())
cout<<"found"<<endl;
}
find(x) | 在 set 中找出數值為 x 的元素。若 x 已經存在則回傳 x 所在的迭代器,否則會回傳 set 的 end 所在位置的迭代器 |
---|---|
count(x) | 若 x 不在 set 中,回傳 0 否則回傳 1 |
lower_bound(x) | 若 set 是遞增排序: 回傳 set 中第一個不小於 x 元素的迭代器 若 set 是遞減排序: 回傳 set 中第一個不大於 x 元素的迭代器 |
upper_bound(x) | 若 set 是遞增排序: 回傳 set 中第一個大於 x 元素的迭代器 若 set 是遞減排序: 回傳 set 中第一個小於 x 元素的迭代器 |
found
#include <iostream>
#include <set>
using namespace std;
int main ()
{
set<int> set1={1,3,5,2,4,6,2};
cout<<set1.count(2)<<endl;
}
1
#include<iostream>
#include<set>
using namespace std;
int main()
{
set<int> set1={1,3,2,4,6};
auto it=set1.lower_bound(5);
cout<<*it<<endl;
}
6
#include<iostream>
#include<set>
using namespace std;
int main()
{
set<int> set1={1,3,2,4,6};
auto it=set1.upper_bound(5);
cout<<*it<<endl;
}
6

參考資料之謝謝老師和陌生人
MAP
MAP
- #include<map>
- 預設鍵值遞增排序(和插入順序無關)
- key(關鍵字) - value(值)
- 不允許key重複(也不能修改)
- 讓搜尋、插入與刪除資料更有效率
• 口 •
!
#include<iostream>
#include<map>
using namespace std;
int main()
{
/*遞增排序
map<鍵值型別,資料型別,less<鍵值型別>> 名稱*/
map<string,int,less<string>> map1={
{"kanae",100},
{"kuzuha",50},
{"kenmochi",1}
};
/*遞減排序
map<鍵值型別,資料型別,greater<鍵值型別>> 名稱*/
map<string,int,greater<string>> map2={
{"kanae",100},
{"kuzuha",50},
{"kenmochi",1}
};
}
MAP的宣告
• _ •
?
kanae 100
kenmochi 1
kuzuha 50
kuzuha 50
kenmochi 1
kanae 100
用ascii碼排序!
• 口 •
!
• _ •
?
MAP的函式之增刪大法
insert(pair<鍵值型別,資料型別>(key,value)); | 插入元素到map,若 key已經存在則不會進行插入 |
---|---|
map[key] | 若在 map中有此 key值: 回傳所對應的 value 若在 map中沒有此 key值: 新增此 key 到 map |
map[key]=value | 若在map中有此key值: 以value取代原本key所對應的資料 若在map中沒有此key值: 新增此key到map |
erase(it) | 刪除map的迭代器it位置的元素 |
erase(key) | 刪除map中鍵值為key的元素,回傳刪除幾個元素 |
clear() | 刪除map中所有元素 |
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<int,string> list={
{1,"送"},
{2,"舊"},
{3,"大"}
};
list.insert(pair<int,string>(4,"順"));
for(auto it:list)
cout<<it.first<<" "<<it.second<<endl;
}
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<int,string> list={
{1,"送舊"},
{2,"大"},
{3,"成功"}
};
list[4];
for(auto it:list)
cout<<it.first<<" "<<it.second<<endl;
}
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<int,string> list={
{1,"送舊"},
{2,"大"},
{3,"成功"}
};
list[4]="罷拖QAQ";
for(auto it:list)
cout<<it.first<<" "<<it.second<<endl;
}
1 送
2 舊
3 大
4 順
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<int,string> list={
{1,"送舊"},
{2,"大"},
{3,"成功"}
};
auto first=list.find(2);
auto last=list.find(3);
list.erase(first,last);
for(auto it:list)
cout<<it.first<<" "<<it.second<<endl;
}
1 送舊
2 大
3 成功
4
1 送舊
2 大
3 成功
4 罷拖QAQ
1 送舊
3 成功
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<int,string> list={
{1,"送舊"},
{2,"大"},
{3,"成功"}
};
auto it=list.erase(2);
list.erase(2);
for(auto it:list)
cout<<it.first<<" "<<it.second<<endl;
cout<<it<<endl;
}
1 送舊
3 成功
1
• 口 •
!
• _ •
?
MAP的函式之讓我找找
find(key) | 在 set 中找出數值為 x 的元素。若 x 已經存在則回傳 x 所在的迭代器,否則會回傳 set 的 end 所在位置的迭代器 |
---|---|
count(key) | 若 x 不在 set 中,回傳 0 否則回傳 1 |
lower_bound(key) | 若 set 是遞增排序: 回傳 set 中第一個不小於 x 元素的迭代器 若 set 是遞減排序: 回傳 set 中第一個不大於 x 元素的迭代器 |
upper_bound(key) | 若 set 是遞增排序: 回傳 set 中第一個大於 x 元素的迭代器 若 set 是遞減排序: 回傳 set 中第一個小於 x 元素的迭代器 |
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<int,string> list={
{5,"送舊"},
{2,"大"},
{1,"大"}
};
auto it=list.find(2);
if(it!=list.end())
cout<<(*it).first<<" "<<it->second<<endl;
for(auto it:list)
cout<<it.first<<" "<<it.second<<endl;
}
2 大
1 大
2 大
5 送舊
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<int,string> list={
{5,"送舊"},
{2,"大"},
{1,"大"}
};
auto it=list.count(2);
cout<<it<<endl;
for(auto it:list)
cout<<it.first<<" "<<it.second<<endl;
}
1
1 大
2 大
5 送舊
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<int,string> list={
{5,"送舊"},
{2,"大"},
{1,"大"}
};
auto it=list.lower_bound(1);
cout<<(*it).first<<" "<<(*it).second<<endl;
for(auto it:list)
cout<<it.first<<" "<<it.second<<endl;
}
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<int,string> list={
{5,"送舊"},
{2,"大"},
{1,"大"}
};
auto it=list.upper_bound(1);
cout<<(*it).first<<" "<<(*it).second<<endl;
for(auto it:list)
cout<<it.first<<" "<<it.second<<endl;
}
1 大
1 大
2 大
5 送舊
2 大
1 大
2 大
5 送舊

參考資料之謝謝老師和陌生人

set&map
By ㄌㄌ
set&map
- 89