Set&Map
C++ STL
講師:陳曉璇
Index
Set
較方便搜尋、插入、刪除資料
簡單介紹一下!
- 不允許重複元素
- 不允許直接修改
宣告
#include <iostream>
#include <set>
using namespace std;
int main(){
//C++ 初始化
set<int> Set1{1,2,3,4,5};
//C-style 陣列初始化
int Set2_arr[5]={1,2,3,4,5};
set<int,greater<int>> Set2(Set2_arr,Set2_arr+5);
//用迭代器訪問元素
for(auto& it:Set1){
cout << it;
}
cout << endl;
for(auto& it:Set2){
cout << it;
}
return 0;
}
//預設:遞增排序
set< 型別 > 名稱;
//遞增排序
set< 型別,less<型別> > 名稱;
//遞減排序
set< 型別,greater<型別> > 名稱;
12345
54321
有 序
&
不重複
#include <iostream>
#include <set>
using namespace std;
int main(){
set<int> Set{5,3,4,2,1,3,5};
//用迭代器訪問元素
for(auto& it:Set){
cout << it;
}
return 0;
}
12345
函 式
儲存空間
#include <iostream>
#include <set>
using namespace std;
int main(){
set<int> Set{5,3,4,2,1,3,5};
if (Set.empty()) {
cout << "empty" << endl;
}
else {
cout << Set.size() << endl;
}
return 0;
}
empty() | 回傳 set 是否為空 |
size() | 回傳 set 內元素數量 |
5
函 式
插入 / 刪除
#include <iostream>
#include <set>
using namespace std;
int main(){
set<int> Set{1,3,5,6,9};
Set.insert(7);
Set.erase(6);
for(auto& it:Set){
cout << it;
}
Set.clear();
cout << endl << Set.empty();
return 0;
}
13579
1
insert(x) | 插入元素 x 到 set ,會檢查是否 x 已經在集合內 |
erase(it) | 刪除 set 的迭代器 it 位置的元素。迭代器 it 的值無法任意指定,只能由開始或結束位置,不斷遞增或遞減到指定位置。 |
erase(x) | 刪除 set 中數值為 x 的元素,回傳刪除幾個元素,回傳值為 0 或 1 。 |
clear() | 刪除 set 中所有元素 |
函 式
搜 尋
#include <iostream>
#include <set>
using namespace std;
int main(){
set<int> Set{1,2,3,4,5};
int n=3;
if( Set.find(n)!=Set.end() ){
Set.erase(n);
}
if( !Set.count(n) ){
Set.insert(n);
}
for(auto it=Set.lower_bound(n);
it!=Set.upper_bound(n);++it){
cout << *it;
}
return 0;
}
find(x) | 在 set 中找出數值為 x 的元素,若 x 已經存在則回傳 x 所在的迭代器,否則會回傳 set 的 end 所在位置的迭代器。 |
count(x) | 計算 set 中數值為 x 的個數,若 x 不在 set 中,則回傳 0 ,否則回傳 1 。 |
lower_bound(x) | 若 set 是遞增排序,則是回傳 set 中第一個不小於 x 元素的迭代器;若 set 是遞減排序,則是回傳 set 中第一個不大於 x 元素的迭代器。 |
upper_bound(x) | 若 set 是遞增排序,則是回傳 set 中第一個大於 x 元素的迭代器;若 set 是遞減排序,則是回傳 set 中第一個小於 x 元素的迭代器。 |
3
Set
Vector
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> Vector{5,3,4,2,1,3,5};
vector<int>::iterator it=Vector.begin()+3;
Vector.erase(it);
//用迭代器訪問元素
for(vector<int>::iterator it=Vector.begin();it!=Vector.end();++it){
cout << *it;
}
return 0;
}
#include <iostream>
#include <set>
using namespace std;
int main(){
set<int> Set{5,3,4,2,1,3,5};
set<int>::iterator it=Set.begin()+3;
Set.erase(it);
//用迭代器訪問元素
for(set<int>::iterator it=Set.begin();it!=Set.end();++it){
cout << *it;
}
return 0;
}
534135
CE
vector / deque
stack / queue
set / map
隨機迭代器
不支援迭代器
雙向迭代器
Map
較方便搜尋、插入、刪除資料
鍵值key - 資料value
簡單介紹一下!
- 不允許重複鍵值
- 不允許直接修改鍵值
宣告
//預設:遞增排序
map< 鍵值型別,資料型別 > 名稱;
//遞增排序
map< 鍵值型別,資料型別,less<鍵值型別> > 名稱;
//遞減排序
map< 鍵值型別,資料型別,greater<鍵值型別> > 名稱;
宣告
#include <iostream>
#include <map>
using namespace std;
int main(){
//預設遞增排序
map<string,int> Map1={
{"zsisc",29},
{"duck",7},
{"penguin",2}
};
//遞減排序
map<string,int,greater<string>> Map2={
{"zsisc",29},
{"duck",7},
{"penguin",2},
{"penguin",10}
};
//用迭代器訪問元素
for (auto& it:Map1){
cout << it.first << " => " << it.second << endl;
}
cout << endl;
for (auto it=Map2.begin();it!=Map2.end();++it){
cout << (*it).first << " => " << (*it).second << endl;
//cout << it->first << " => " << it->second << endl;
}
return 0;
}
duck => 7
penguin => 2
zsisc => 29
zsisc => 29
penguin => 2
duck => 7
函 式
儲存空間
#include <iostream>
#include <map>
using namespace std;
int main(){
map<string,int> Map={
{"zsisc",29},
{"duck",7},
{"penguin",2}
};
if (Map.empty()) {
cout << "empty" << endl;
}
else {
cout << Map.size() << endl;
}
return 0;
}
empty() | 回傳 map 是否為空 |
size() | 回傳 map 內元素數量 |
3
函 式
插入 / 刪除
insert(pair<鍵值型別,資料型別>(key,value)) | 插入元素到 map ,會檢查是否 key 已經在集合內,若 key 已經存在則不會進行插入。 |
map[key] | 回傳key 值所對應的 value;若在 map 中沒有此 key 值,就會新增此 key 到 map 。 |
map[key]=value | 以 value 取代原本 key 所對應的資料;若在 map 中沒有此 key 值,就會新增此 key 到 map 。 |
erase(it) | 刪除 map 的迭代器 it 位置的元素,迭代器 it 的值無法任意指定,只能由開始或結束位置,不斷遞增或遞減到指定位置。 |
erase(key) | 刪除 map 中鍵值為 key 的元素,回傳刪除幾個元素,回傳值為 0 或 1 。 |
clear() | 刪除 map 中所有元素 |
函 式
插入 / 刪除
#include <iostream>
#include <map>
using namespace std;
int main(){
map<string,int> Map;
Map["zsisc"]=29;
Map["duck"]=7;
Map["penguin"]=2;
Map.insert(pair<string,int>("duck",14));
for (auto& it:Map){
cout << it.first << " => " << it.second << endl;
}
Map["duck"]=14;
cout << endl;
for (auto& it:Map){
cout << it.first << " => " << it.second << endl;
}
return 0;
}
duck => 7
penguin => 2
zsisc => 29
duck => 14
penguin => 2
zsisc => 29
函 式
搜 尋
find(key) | 在 map 中找出鍵值為 key 的元素,會檢查是否 key 已經在 map 內,若 key 已經存在則回傳 key 所在的迭代器,否則會回傳 map 的 end 所在位置的迭代器。 |
count(key) | 計算 map 中鍵值為 key 的個數,若 key 不在 map 中,則回傳 0 ,否則回傳 1 。 |
lower_bound(key) | 若 map 是遞增排序,則是回傳 map 中第一個鍵值不小於 key 元素的迭代器;若 map 是遞減排序,則是回傳 map 中第一個鍵值不大於 key 元素的迭代器。 |
upper_bound(key) | 若 map 是遞增排序,則是回傳 map 中第一個鍵值大於 key 元素的迭代器;若 map 是遞減排序,則是回傳 map 中第一個小於 key 元素的迭代器。 |
函 式
搜 尋
#include <iostream>
#include <map>
using namespace std;
int main(){
map<string,int> Map={
{"zsisc",29},
{"duck",7},
{"penguin",2}
};
string n="penguin";
if(Map.find(n)!=Map.end()){
Map[n]=Map["duck"];
}
if(!Map.count(n)){
Map.erase(n);
}
for(auto it=Map.lower_bound(n);it!=Map.upper_bound(n);++it){
cout << (*it).first << "=>" << (*it).second;
}
return 0;
}
penguin=>7
Kahoot!
– 參考資料
Set & Map
By shaunna163
Set & Map
- 105