Standard Template Library标准模板库专项复习总结(一)
生活随笔
收集整理的這篇文章主要介紹了
Standard Template Library标准模板库专项复习总结(一)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
看了看博客園的申請時間也一年多了...想想自己一年多以來一直處于各種劃水狀態(tài),現(xiàn)在又要面臨ACM的沖擊...
還是要抓緊時間趕緊復習一下了- -畢竟校園新生賽還是有獎金的..
?
1.棧
?
先進后出(LIFO)表
頭文件:#include<stack>
變量的定義:stack<TYPE>StackName
成員函數(shù): bool empty() 棧為空返回true,否則返回false void pop() 刪除棧頂元素 void push(const TYPE &val) 進棧 size_type size() 返回棧的數(shù)目 TYPE& top() 查看棧首?
2.動態(tài)數(shù)組
頭文件:#include<vector>
變量的定義:vector<TYPE>vectorName
成員函數(shù): TYPE& size() 返回數(shù)組的數(shù)目 bool empty() 數(shù)組為空返回true,否則返回false void clear() 清空數(shù)組 * begin() 返回第一個數(shù)據(jù)的地址 * end() 返回最后一個數(shù)據(jù)的地址 void pop_back() 刪除最后一個數(shù)據(jù) * insert(a,b) 在a位置插入b void insert(a,n,b) 在a位置插入n個b void insert(a,beg,end) 在a位置插入beg到end之間的數(shù)據(jù) void swep(vector) 互換兩個vector3.映射
頭文件:#include<map>
變量的定義:map<TYPE,TYPE>mapName
map<key,elem> 一個map,以less<>為排序準則map<key,elem,op> 一個map,以op為排序準則另外需要定義迭代器(iterator)
map<string,float>::iterator pos;類似于指針,解釋一下就是:
當?shù)鱬os指向map中的某個元素,表達式pos->first獲得該元素的key,表達式pos->second獲得該元素的value舉個例子:(摘自https://blog.csdn.net/shawn_hou/article/details/38035577)
#include <map> #include <string> #include <iostream> using namespace std; int main() {map<int, string> mapStudent;mapStudent.insert(pair<int, string>(1, "student_one"));mapStudent.insert(pair<int, string>(2, "student_two"));mapStudent.insert(pair<int, string>(3, "student_three"));map<int, string>::iterator iter;for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)cout<<iter->first<<' '<<iter->second<<endl; }?
成員函數(shù): iterator begin() 返回指向第一個元素的迭代器 iterator end() 返回指向最后一個元素的迭代器 void clear() 清空map bool empty() 返回是否map為空 void insert() 插入 TYPE& size() 返回map大小*也可以直接使用pair類型輸入
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<map> 5 #include<vector> 6 #include<queue> 7 #define MAXN 8 using namespace std; 9 int n,x,y; 10 int main(){ 11 ios::sync_with_stdio(false); 12 map<int,int>m; 13 m.insert(pair<int,int>(1,2)); 14 cin>>n; 15 for(register int i=1;i<=n;i++){ 16 cin>>x>>y; 17 pair<int,int> p (x,y); 18 m.insert(p); 19 } cout<<endl; 20 for(register map<int,int>::iterator i=m.begin();i!=m.end();i++){ 21 pair<int,int>it=*i; 22 cout<<it.first<<" "<<it.second<<endl; 23 } 24 return 0; 25 } 26 原文:https://blog.csdn.net/NOIAu/article/details/72923307?------對于沒有數(shù)據(jù)結(jié)構(gòu)基礎的小白,比如不知道什么是迭代器什么是pair可以自行百度
總而言之,map=pair+set
#include<cstdio> #include<iostream> #include<map> using namespace std; int main(){map<string,float> c; c.insert(make_pair("Cafe",7.75));c.insert(make_pair("Banana",1.72));c["Wine"]=15.66;map<string,float>::iterator pos;for(pos=c.begin();pos!=c.end();pos++){cout<<pos->first<<" "<<pos->second<<endl; }return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/Fylsea/p/9910872.html
總結(jié)
以上是生活随笔為你收集整理的Standard Template Library标准模板库专项复习总结(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 荣耀80以旧换新荣耀90便宜多少
- 下一篇: 茅台酒为什么叫飞天?