# STL (and struct)
>https://hackmd.io/@adia39431/HyfApsrByg#%E5%9B%9B%E3%80%81STL%E6%BC%94%E7%AE%97%E6%B3%95
---
## very important:請記得如果有多筆數據,在每次loop完後要把容器要clear掉!! !! !! !! (我肯定沒有花了半個小時在找)
### **Struct**
```cpp
#include <bits/stdc++.h>
using namespace std;
struct pos{
int x;
int y;
string str;
}a;
//宣告a變數,記得加分號
int main(){
pos b;
a.x = 32;
a.y = 45;
a.str = "a";
b.x = 23;
b.y = 15;
b.str = "b";
pos c{13, 25, "c"};
cout << a.x << " " << a.y << " " << a.str << endl;
cout << b.x << " " << b.y << " " << b.str << endl;
cout << c.x << " " << c.y << " " << c.str << endl;
}
```
最後輸出會是:
```
32 45 a
23 15 b
13 25 c
```
* **自訂義加減、比大小:**
```cpp
#include<bits/stdc++.h>
using namespace std;
struct my_struct{
int x;
double y;
string s;
//可以用在sort
bool operator <(my_struct b){
return x < b.x; // 定義小於為 x 較另一個人的 x 小
}
my_struct operator +(my_struct b){
return {x+b.x, y+b.y, s+b.s};
}
};//要有分號
int main(){
my_struct a;
a.x = 45;
a.y = 55;
a.s = "a";
my_struct b{25,43,"b"};
//比大小
if(a < b){
cout << "A is smaller than B\n";
}
else{
cout << "A is bigger than B\n";
}
//相加
a = a + b; // 不可寫 a += b 因為你只定義 + 沒有定義 +=
cout<< a.x << " " << a.y << " " << a.s << endl;
}
```
最後輸出會是:
```
A is bigger than B
70 98 ab
```
---
### **vector**
```cpp
#include<bits/stdc++.h>
using namespace std;
vector <int> v;
int main(){
vector<int> v;
v.push_back(10); //後方加入
v.emplace_back(45); //後方加入
cout << v[0] << " " << v[1] << endl;
v.pop_back(); //移除後方
cout << v[0] << endl;
cout << endl;
//利用
v = {3, 1, 2};
//取得大小
cout << "大小:" << v.size() << '\n';
//反轉
reverse(v.begin(), v.end());
cout << "反轉:";
for (int itr:v) cout << itr << " ";
cout << endl;
//排序
sort(v.begin(), v.end());
cout << "排序:";
for (int itr:v) cout << itr << " ";
cout << endl;
//是否為空
v = {};
cout << "是否為空:";
if(v.empty()) cout << "Empty!\n";
cout << endl;
}
```
最後輸出會是:
```
10 45
10
大小:3
反轉:2 1 3
排序:1 2 3
是否為空:Empty!
```
---
### **pair**
```cpp
#include<bits/stdc++.h>
using namespace std;
pair<int,int> p[1005];
int main(){
p[0].first = 2; p[0].second = 3;
p[1].first = 1; p[1].second = 2;
p[2].first = 2; p[2].second = 4;
p[3].first = 4; p[3].second = 3;
sort(p, p+3);
for (int i=0; i<=3; ++i) cout << p[i].first << " " << p[i].second << endl;
}
```
最後輸出會是:
```
1 2
2 3
2 4
4 3
```
---
### **set**
```cpp
#include<bits/stdc++.h>
using namespace std;
set<int> s; //同一個東西只能存一次
int main(){
s.insert(5); //加入
s.insert(6);
s.insert(4);
s.insert(7);
s.erase(5); //移除
//回傳該元素的 iterator,若 set 內部無該元素,則回傳 end()。
cout << "找到:" << *s.find(7) << endl;
cout << "upper_bound(第一個 > 4):" << *s.upper_bound(4) << endl; // 第一個 >
cout << "lower_bound(第一個 >= 4):" << *s.lower_bound(4) << endl; // 第一個 >=
cout << endl;
//輸出set-1
for(auto iter = s.begin(); iter != s.end(); iter++) cout << *iter << " ";
cout << endl;
//輸出set-2
for (int iter:s) cout << iter << " ";
cout << endl;
}
```
最後輸出會是:
```
找到:7
upper_bound(第一個 > 4):6
lower_bound(第一個 >= 4):4
4 6 7
4 6 7
```
---
### **stack**
```cpp
#include<bits/stdc++.h>
using namespace std;
stack<int> st;
//想像是一個箱子,然後你只能從最上方拿出東西
int main(){
st.push(1); // 從後方加入元素
st.push(2);
st.push(5);
cout << st.top() << endl; // 查看最前方元素
cout << st.size() << endl; // 查看queue大小
st.pop(); // 從前方移除元素
cout << st.top() << endl; // 查看最前方元素
cout << st.size() << endl; // 查看queue大小
}
```
最後輸出會是:
```
5
3
2
2
```
---
### **queue**
```cpp
#include<bits/stdc++.h>
using namespace std;
queue<int> q;
//想像是一個排隊的隊伍,從後進入,前方出來
int main(){
q.push(1); // 從後方加入元素
q.push(2);
q.push(5);
cout << q.front() << endl; // 查看最前方元素
cout << q.size() << endl; // 查看queue大小
q.pop(); // 從前方移除元素
cout << q.front() << endl; // 查看最前方元素
cout << q.size() << endl; // 查看queue大小
q.pop();
q.pop();
if(q.empty()) cout << "Empty!\n";
}
```
最後輸出會是:
```
1
3
2
2
Empty!
```
---
### **map**
```cpp
#include<bits/stdc++.h>
using namespace std;
map<string, int> mp;
//map 能夠將一個鍵(key),對應到一個值(value)。
int main(){
mp["A"] = 5;
cout << mp["A"] << endl;
mp.erase("A");
//map的值可以放很多東西,像是stack, pair等
cout << endl;
mp["First"] = 2;
mp["Second"] = 4;
mp["Third"] = 5;
mp["Fourth"] = 1;
mp["Fifth"] = 6;
//輸出
for (auto i:mp) cout << i.first << " " << i.second << endl;
}
```
最後輸出會是:
```
5
Fifth 6
First 2
Fourth 1
Second 4
Third 5
```
---
### **priority_queue**
```cpp
#include<bits/stdc++.h>
using namespace std;
priority_queue<int> pq; //最上方會是最大的
priority_queue<int, vector<int>, greater<int>> pq_small; //最上方會是最小的
//最大要填入 less<int>,最小要填入 greater<int>
int main(){
pq.push(1);
pq.push(5);
pq.push(2);
pq.push(4);
//pq 和 queue 都有size和empty
cout << pq.size() << endl;
if (pq.empty() == false) cout << "Not Empty!" << endl;
//pq最上方要用top,和queue不同
cout << pq.top() << endl;
pq.pop(); //pop 5
pq.pop(); //pop 4
cout << pq.top() << endl;
}
```
最後輸出會是:
```
4
Not Empty!
5
2
```
---
### **deque**
```cpp
#include<bits/stdc++.h>
using namespace std;
deque<int> dq;
int main(){
dq.push_back(1);
dq.push_front(5);
cout << dq.size() << endl;
cout << dq[0] << " " << dq[1] << endl;
dq.pop_back();
dq.pop_front();
if (dq.empty()) cout << "Empty!" << endl;
//和vector差不多,像是upper_bound、lower_bound都可以用,
//只是可同時從前方和後方加入
//*比vector慢很多
}
```
最後輸出會是:
```
2
5 1
Empty!
```
---
### **bitset**
```cpp
#include<bits/stdc++.h>
using namespace std;
bitset<100> b;
int main(){
b[5] = 0;
b[4] = 1;
b[6] = 1;
cout << b.count() << endl; // 回傳 bitset 中 1 的個數。
b.set(); // 將所有 bit 設為 1
cout << b[1] << endl;
b.reset(); // 將所有 bit 設為 0
cout << b[1] << endl;
}
```
最後輸出會是:
```
2
1
0
```
---
**ok bye**