STL在競賽中的應用
阿新 • • 發佈:2021-08-01
很多比較簡單的東西都沒寫
STL在競賽中的應用
概論
vector
vector 是不定長陣列容器。其陣列長度的擴增策略是每次乘 2
其操作方式如下:
list
連結串列容器
string
queue & deque
stack
priority_queue
優先佇列,通過堆來實現,能夠保證隊首元素為最大(小)值,支援插入,刪除操作(\(O(log(n))\)) ,也支援取隊首元素、彈出隊首元素(\(O(1)\))
使用方法如下:
#include<queue> #include<iostream> using namespace std; struct point{ int x,y,z; const bool operator <(const point &temp)const{ if(x!=temp.x) return x<temp.x; if(y!=temp.y) return y<temp.y; if(z!=temp.z) return z<temp.z; return false; } const bool operator >(const point &temp)const{ return temp<*this; } void output(){ cout<<x<<" "<<y<<" "<<z<<endl; } }; int main(){ /*宣告*/ priority_queue<point> pq_0; /*大根堆構造*/ priority_queue<point,vector<point>,greater<point>> pq_1;//小根堆,過載>方法 priority_queue<point,vector<point>,less<point>> pq_2;//大根堆,過載<方法 /*訪問*/ if(!pq_0.empty()){ point temp=pq_0.top(); temp.output(); } /*清空*/ while(pq_0.empty()) pq_0.empty(); }
各種比較運算子過載方法(通過\(<\)延伸):
bool operator <(const point &temp)const{ if(x!=temp.x) return x<temp.x; if(y!=temp.y) return y<temp.y; if(z!=temp.z) return z<temp.z; return false; } bool operator >(const point &temp)const{ return temp<*this; } bool operator <=(const point &temp)const{ return !(temp>*this); } bool operator >=(const point &temp)const{ return !(*this<temp); } bool operator ==(const point &temp)const{ return (*this<=temp) && (*this>=temp); }