一、vector动态数组

用法:

尾部添加:push_back()

元素个数:size()

是否为空:empty()

在第i个元素前面插入k:insert(a.begin()+i,k)

删除尾部元素:pop_back()

删除区间:eraser(a.begin()+i,a.begin()+j)  删除区间[i,j-1]的元素

删除元素:eraser(a.begin()+2)  删除第三个元素

清空:clear()

反转:reverse()

排序:sort(a.begin(),a.end())

接下来练一下:圆桌问题

​http://acm.hdu.edu.cn/showproblem.php?pid=4841​

#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;
vector<int>table;
int main()
{
int n,m,i,j;
while(cin>>n>>m)
{
table.clear();//清空数组
for(i=0;i<2*n;i++)
table.push_back(i);//放进数组
int pom=0;
for(i=0;i<n;i++)
{
pom=(pom+m-1)%table.size();
table.erase(table.begin()+pom);//清除那个人
}
j=0;
for(i=0;i<2*n;i++)
{
if(!(i%50)&&i)
cout<<endl;
if(j<table.size()&&i==table[j])
{
j++;
cout<<"G";
}
else
{
cout<<"B";
}
}
cout<<endl<<endl;
}
}

二、栈和stack

用法:

入栈:push()

取栈顶:top()

删除栈顶元素:pop()

栈为空:empty()

栈内元素:size()

来个小例子:翻转字符串

​http://acm.hdu.edu.cn/showproblem.php?pid=1062​

#include<iostream>
#include<stack>
#include<cstdio>
using namespace std;
int main()
{
int n,i,j;
char c;
cin>>n;
getchar();
while(n--)
{
stack<char>s;
while(true)
{
c=getchar();
if(c=='\n'||c==' '||c==EOF)
{
while(!s.empty())
{
cout<<s.top();
s.pop();
}
if(c==' ')
cout<<" ";
if(c=='\n'||c==EOF)
break;
}
else
s.push(c);
}
cout<<endl;
}
return 0;
}

三、队列和queue

用法:

入队列:push()

返回队首:front()

返回队尾:back()

删除队首:pop()

返回元素个数:size()

判断队列为空:empty()

来个案例:

​http://acm.hdu.edu.cn/showproblem.php?pid=1702​

#include<iostream>
#include<queue>
#include<stack>
#include<string>
using namespace std;
int main()
{
int t,n,x;
string s,m;
cin>>t;
while(t--)
{
cin>>n>>s;
queue<int>q;
stack<int>stac;
if(s=="FIFO")
{
while(n--)
{
cin>>m;
if(m=="IN")
{
cin>>x;
q.push(x);
}
else if(m=="OUT")
{
if(q.empty())
cout<<"None"<<endl;
else
{
cout<<q.front()<<endl;
q.pop();
}
}
}
}
else if(s=="FILO")
{
while(n--)
{
cin>>m;
if(m=="IN")
{
cin>>x;
stac.push(x);
}
else if(m=="OUT")
{
if(stac.empty())
cout<<"None"<<endl;
else
{
cout<<stac.top()<<endl;
stac.pop();
}
}
}
}
}
return 0;
}

四、优先队列(priority_queue)

用法:

返回最高级的元素值:top()

删除最高级元素:pop()

插入新元素:push()

例子:看病要排队

​http://acm.hdu.edu.cn/showproblem.php?pid=1873​

 

#include<iostream>
#include<queue>
#include<string>
#include<stdio.h>
using namespace std;
struct node
{
int pre;
int id;
int kk;
bool operator <(const node &a)const
{
if(pre!=a.pre)
return pre<a.pre;
else
return kk>a.kk;
}
}nod;
int main()
{
int n,i,j=1,a,b,k=1;
string s;
while(cin>>n&&n!=EOF)
{
priority_queue<node> pq[5];
j=1,k=1;
for(i=1;i<=n;i++)
{
cin>>s;
if(s=="IN")
{
cin>>a>>nod.pre;
nod.id=j++;
nod.kk=k++;
pq[a].push(nod);
}
else
{
cin>>a;
if(pq[a].empty())
{
cout<<"EMPTY"<<endl;
}
else
{
cout<<pq[a].top().id<<endl;
pq[a].pop();
}
}
}
}
}