NCTU_Valkyrie
[lɪŋkt lɪst]
value
next
value
next
value
next
value
next
head
NULL
value
next
value
next
head
NULL
value
next
new
value
next
value
next
head
NULL
value
next
new
value
next
value
next
head
NULL
value
next
new
value
next
value
next
head
NULL
value
next
del
value
next
value
next
head
NULL
value
next
del
value
next
value
next
head
NULL
value
next
head
NULL
pre
value
next
pre
value
next
pre
value
next
pre
NULL
insert
delete
push
stack
stack
pop
top
top
push
front
front
back
back
pop
back
back
front
front
undirected vs directed
acyclic vs cyclic
simple vs multi
5, 4, 6, 3, 8, 7
5
root
5
root
4
5
root
4
6
6
root
4
5
6
root
4
5
4
3
6
root
4
5
4
3
8
6
root
4
5
8
3
4
8
root
4
5
6
3
4
8
root
4
5
6
3
4
7
8
root
4
7
6
3
4
5
http://www.cplusplus.com/reference/
所以以下跳過不講。(幫QQ
more than pointer
vector<int> v;
vector<int>::iterator it;
//用iterator走完整個vector
for(it = v.begin(); it != v.end(); it++){
cout << *it << endl;
}
//用index走完整個vector
for(int i = 0; i < v.size(); i++){
cout << v[i] << endl;
}
begin() : 指向 Container 中的第一個元素
end() : 指向 Container 的結尾(並不是最後一個元素)
stl_function(first, last)
前閉後開區間 [first,last)
需要區間資料的 STL 函數都是
將兩個元素綁在一起的資料結構
鍵值對(Key-Value pair)
用Key來尋找對應的Value
將Key map 到 Value
Key唯一,不會重複存在
實作:
一個神奇的自平衡二元搜索樹(通常以紅黑樹實作)
高度為O(log N)
m[Key] = Value 、 m.insert() 複雜度O(log N)
m.find() 複雜度O(log N)
map<int, int> m;
map<int, int>::iterator it;
for(it = m.begin(); it != m.end(); it++)
{
cout << (*it).first << (*it).second << endl;
}
map<int, int> m;
沒有Value的map
僅插入、刪除Key 檢查Key是否存在
Key唯一,不會重複存在
set<T> s;
s.insert((T) object)
s.erase((T) object)
s.erase(first, last)
s.begin()
s.end()
s.size()
s.empty()
s.clear()
Compared-Based Sort
複雜度 O(N log N)
反轉 Container 內的所有元素
可使用 reverse 的 Container : vector, deque, string, list
reverse(first, last);
反轉字串string str
reverse(str.begin(), str.end());
複雜度 O(N)
lower_bound(first, last, (T) object)
限制:Container 內的元素必須有序,使用才有意義
回傳第一個大於等於輸入元素的iterator
如果沒有,則回傳end()
vector<int> v;
sort(v.begin(), v.end());
auto it = lower_bound(v.begin(), v.end(), 10);
複雜度 O(log N)
回傳第一個大於輸入元素的iterator
如果沒有,則回傳end()
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
cin.tie(0);
cin.sync_with_stdio(0);
int n, q, now;
cin >> n;
vector<int> v;
for(int i = 0; i < n; ++i){
cin >> now;
v.push_back(now);
}
sort(v.begin(), v.end());
cin >> q;
while(q--){
cin >> now;
cout << upper_bound(v.begin(), v.end(), now) - v.begin() << endl;
}
}
template<class T>
struct Node<T>
{
T value;
struct Node<T>* parent;
vector<struct Node<T>*> children;
};
1
2
3
4
5
6
7
Stack
1
2
3
4
5
6
7
1
Stack
1(1)
2
3
4
5
6
7
2
Stack
3
1(1)
2
3(2)
4
5
6
7
2
Stack
6
7
1(1)
2
3(2)
4
5
6
7(3)
2
Stack
6
1(1)
2
3(2)
4
5
6(4)
7(3)
2
Stack
1(1)
2(5)
3(2)
4
5
6(4)
7(3)
4
Stack
5
1(1)
2(5)
3(2)
4
5(6)
6(4)
7(3)
4
Stack
1(1)
2(5)
3(2)
4(7)
5(6)
6(4)
7(3)
Stack
1
2
3
4
5
6
7
Queue
1
1(1)
2
3
4
5
6
7
Queue
2
3
1(1)
2(2)
3
4
5
6
7
Queue
3
4
5
1(1)
2(2)
3(3)
4
5
6
7
Queue
4
5
6
7
1(1)
2(2)
3(3)
4(4)
5
6
7
Queue
5
6
7
1(1)
2(2)
3(3)
4(4)
5(5)
6
7
Queue
6
7
1(1)
2(2)
3(3)
4(4)
5(5)
6(6)
7
Queue
7
1(1)
2(2)
3(3)
4(4)
5(5)
6(6)
7(7)
Queue