Set&Map

C++ STL

林芊妘

Set的用途

  1. 快速查詢元素是否存在
  2. 儲存唯一值(值不重複)
  3. 去除陣列中重複的值
  4. 預設遞增排序

1  17  25  49  89 117

Map的用途

  1. 一對一的映射 key->value
  2. 以鍵值(key)去搜尋對映到的資料(value)
  3. 不允許鍵值重複
  4. 鍵值呈現以排序狀態

A

B

C

D

apple

banana

cherry

durian

Set

Set常用函式

begin() 回傳第一個元素
end() 回傳最後一個元素的下一個元素,表示資料已達到結尾
insert(x) 插入x元素至集合中,若值存在,元素不會插入,回傳元素在集合中的迭代器
erase(x) 刪除數值為x的元素,並回傳刪除幾個(0或1)
erase(it) 刪除迭代器中it位置的元素
clear() 刪除集合中所有元素

函式庫 #include<set>

set無法隨機讀取某元素,要透過迭代器來讀取元素

迭代器的功能就像指標一樣,

可以走訪STL容器(vector,set,map等)裡每個元素

兩者皆可用*取值

#include <iostream>
#include <set>
using namespace std;
int main(){
    set<int> set1={32,38};
    set<int>::iterator it;
    it=set1.begin();
    cout<<*it;
    return 0;
}

32

迭代器迴圈

#include <iostream>
#include <set>
using namespace std;
int main(){
    set<int> set1={32,38};
    set<int>::iterator it;
    for(set<int>::iterator it=set1.begin();it!=set1.end();it++)
    {
        cout<<*it<<" ";
    }
    return 0;
}

迭代器it的值無法任意指定,只能由開始和結束位置,不斷遞增或遞減到指定位置

32 38

auto

編譯時自動推導變數型別

auto x=1;
int x=1;
auto x;
set<int>::iterator it=set1.begin();
auto it=set1.begin();

Set宣告

#include<iostream>
#include<set>
using namespace std;
int main()
{
    set<int> set1={2,9,8,5,7};
    for(auto it:set1){
        cout<<it;
    }
    return 0;
}

遞增排序 25789

:set1 表示for迴圈會從set1這個容器中,依序拿出每個元素,指定給it

#include<iostream>
#include<set>
using namespace std;
int main()
{
    set<int> set1={2,3,7,7,5,3,9};
    for(auto it:set1){
        cout<<it;
    }
    return 0;
}

不會有重複的值 23579

Set宣告

遞增排序:set<資料型別,less <資料型別> > 名稱

#include<iostream>
#include<set>
using namespace std;
int main(){
    set<int,less<int> > set1={1,3,4,2,5,6};
    for(auto it:set1) cout<<it<<" ";
}

1 2 3 4 5 6

遞減排序:set<資料型別,greater <資料型別> > 名稱

#include<iostream>
#include<set>
using namespace std;
int main()
{
    set<int,greater<int> > set1={1,3,4,2,5,6};
    for(auto it:set1) cout<<it<<" ";
}

6 5 4 3 2 1

insert / erase / clear

#include <iostream>
#include <set>
using namespace std;
int main(){
    set<int> set1={31,32,37,38};
    set1.insert(115);
    set1.insert(116);
    for(auto it:set1) cout<<it<<" ";
    cout<<endl;
    
    set1.erase(31);
    set1.erase(37);
    set1.erase(115);
    for(auto it:set1) cout<<it<<" ";
    cout<<endl;
    
    set1.erase(set1.begin());
    set1.erase(set1.begin());
    for(auto it:set1) cout<<it<<" ";
    cout<<endl;
    set1.clear();
    if(set1.empty()) cout<<"0";
    return 0;
}

31 32 37 38 115 116

32  38  116

116

0

find(x)

在集合中找出數值為x的元素,若x存在,回傳x所在的迭代器,若x不存在,回傳end所在的迭代器

#include<iostream>
#include<set>
using namespace std;
int main()
{
    set<int> set1={2,3,5,7,9};
    int n;
    bool find=0;
    cin>>n;
    if(set1.find(n)!=set1.end())
    {
        find=1;
        cout<<find;
    }
    else cout<<find;
    return 0;
}

n=4

0

n=7

1

count(x)

計算集合中數值為x的數量,x不在集合中回傳0;x在集合中回傳1(set內的值不重複)

#include<iostream>
#include<set>
using namespace std;
int main()
{
    set<int> set1={2,3,5,7,9,2};
    int n;
    cin>>n;
    cout<<set1.count(n);
    return 0;
}

n=2

1

n=8

0

Map

Map宣告

map<鍵值型別,資料型別> 變數名稱

map<鍵值型別,資料型別,less<鍵值型別> > 變數名稱

map<鍵值型別,資料型別,greater<鍵值型別> > 變數名稱

#include<iostream>
#include<map>
using namespace std;
int main()
{
    map<string,int> map1;
    map1={
        {"zsisc",31},
        {"cmioc",31},
        {"ckcsc",37},
        {"infor",37}
    };
    for(auto it:map1) 
        cout<<it.first<<" "<<it.second<<endl;
    return 0;
}

ckcsc 37
 cmioc 31

infor 37

zsisc 31

ASCII碼排序

變數名稱.insert(pair<鍵值型別,資料型別>(插入鍵值,插入資料);

#include<iostream>
#include<map>
using namespace std;
int main(){
    map<string,int> map1;
    map<string,int>::iterator it;
    map1={
        {"zsisc",31},
        {"cmioc",31},
        {"ckcsc",37},
        {"infor",37}
    };
    map1.insert(pair<string,int>("IZCC",115));
    for(auto it:map1) 
        cout<<it.first<<" "<<it.second<<endl;
    return 0;
}

IZCC 115
ckcsc 37
 cmioc 31

infor 37

zsisc 31

插入元素  map  key 已經存在則不會進行插入,會回傳key在map 所在的迭代器

insert

Map[key]

37

IZCC 0
ckcsc 37
 cmioc 31

infor 37

zsisc 31

若map中有key值,回傳value;

若沒有則新增key,value會以預設建構子產生value

#include<iostream>
#include<map>
using namespace std;
int main(){
    map<string,int> map1;
    map1={
        {"zsisc",31},
        {"cmioc",31},
        {"ckcsc",37},
        {"infor",37}
    };
    cout<<map1["infor"]<<endl;
    map1["IZCC"];
    for(auto it:map1){
        cout<<it.first<<" "<<it.second;
        cout<<endl;
    }
    return 0;
}

Map[key]=value

IZCC 116
ckcsc 37
 cmioc 31

infor 37

zsisc 32

若在 map 中有此 key 值,以 value 取代原本 key 所對應的資料;若在 map 中沒有此 key 值,就會新增此 key  map ,此 key 值對應到 value 

#include<iostream>
#include<map>
using namespace std;
int main(){
    map<string,int> map1;
    map1={
        {"zsisc",31},
        {"cmioc",31},
        {"ckcsc",37},
        {"infor",37}
    };
    map1["zsisc"]=32;
    map1["IZCC"]=116;
    for(auto it:map1){
        cout<<it.first<<" "<<it.second;
        cout<<endl;
    }
    return 0;
}

find / erase

cmioc 31

infor 37

zsisc 32

#include<iostream>
#include<map>
using namespace std;
int main(){
    map<string,int> map1;
    map1={
        {"zsisc",31},
        {"cmioc",31},
        {"ckcsc",37},
        {"infor",37}
    };
    auto first=map1.find("ckcsc");
    map1.erase(first);
    for(auto it:map1){
        cout<<it.first<<" "<<it.second;
        cout<<endl;
    }
    return 0;
}

題目

ckcsc 37
 cmioc 31

infor 37

zsisc 31

IZCC 116

ckcsc 38
 cmioc 32

infor 38

zsisc 32

ZeroJudge課程裡也有題目

記得去做!!!

下禮拜是上機考喔

陳千億說要放的-她花了千億小時做的

6/12 社師!!!問他問題!

Kahoot

Made with Slides.com