經常使用的STL有html
stack,queue,map,vectorc++
這些都是封裝好的容器,容器,顧名思義,能夠存聽任一類型,包括結構體類型。數組
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct Node 4 { 5 int id; 6 int val; 7 Node(int id,int val):id(id),val(val){} 8 }; 9 stack<Node >_stack; 10 11 int main() 12 { 13 //調用棧頂的id元素 14 _stack.push(Node(1,2)); 15 int id=_stack.top().id; 16 cout<<id<<endl; 17 }
最經常使用的是vector容器,在圖論中,當頂點數較多時,以致於數組開不下,或者是頂點多,邊數少,用數組會MLE,這時就能夠選擇用vector容器,很是好用的一個容器。數據結構
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb push_back 4 const int maxV=1e5+50;//最大的頂點數 5 struct Node 6 { 7 int to; 8 int weight; 9 Node(int to,int weight):to(to),weight(weight){} 10 }; 11 vector<Node >G[maxV]; 12 void addEdge(int u,int v,int w)//加邊操做 13 { 14 G[u].pb(Node(v,w));//加入一條 u -> v 的邊,且權重爲 w 15 }
其實,用嵌套map也能夠高效的存儲圖中的邊,且佔用的內存也比數組小。ide
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 map<int ,map<int ,int > >G; 5 6 int main() 7 { 8 G.clear(); 9 for(int i=1;i <= 5;++i) 10 { 11 int u,v,w; 12 scanf("%d%d%d",&u,&v,&w); 13 G[u][v]=w;//有向圖 u - > v , 權重爲 w 14 } 15 //嵌套map的遍歷 16 //遍歷誰就用誰的迭代器 17 map<int ,map<int ,int > >::iterator it; 18 for(it=G.begin();it != G.end();++it) 19 { 20 int u=it->first; 21 for(map<int ,int >::iterator iter=G[u].begin();iter != G[u].end();++iter) 22 { 23 int v=iter->first; 24 int w=iter->second; 25 cout<<u<<"->"<<v<<' '<<w<<endl;//u->v 權重爲 w 26 } 27 } 28 return 0; 29 }
圖的存儲方式還有一種比較好用的數據結構,那就是鏈式前向星。spa
題目一覽表 | 來源 | 考察知識點 | 完成時間 |
A 2559 Largest Rectangle in a Histogram | poj | 棧 | 2018.10.12 |