我是想用map存数组,但是全错。
检查了半天发现map里面的内容全对,但是一用begin()和end()就出现错误。不是说map会自动排序吗?
#include <iostream>
#include <map>
using namespace std;
map<int,int> mp;
int main()
{
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
if(!mp.count(x))
mp[x]=1;
else
mp[x]++;
}
for(int i=1;i<=q;i++)
{
int l,x;
cin>>l>>x;
bool pass=false;
if(l==1)
{
if(mp.count(x))
{
mp[x]--;
if(mp[x]==0)
mp.erase(x);
}
else
pass=true;
}
else if(l==2)
{
if(mp.count(x))
mp[x]++;
else
mp[x]=1;
}
if(!pass)
{
std::map<int,int>::iterator it1,it2;
it1=mp.begin();
it2=mp.end();
cout<<(it2->first-it1->first)*2<<"\n";
}
}
return 0;
}