现有一整数集(允许有重复元素),初始为空。我们定义如下操作:
add x 把x加入集合
del x 把集合中所有与x相等的元素删除
ask x 对集合中元素x的情况询问
对每种操作,我们要求进行如下输出。
add 输出操作后集合中x的个数
del 输出操作前集合中x的个数
ask 先输出0或1表示x是否曾被加入集合(0表示不曾加入),再输出当前集合中x的个数,中间用空格格开。
输入
第一行是一个整数n,表示命令数。0<=n<=100000。
后面n行命令,如Description中所述。
输出
共n行,每行按要求输出。
样例输入
7
add 1
add 1
ask 1
ask 2
del 2
del 1
ask 1
样例输出
1
2
1 2
0 0
0
2
1 0
提示
Please use STL’s set and multiset to finish the task
以下是我的代码,求大家帮忙看看
#include<iostream>
#include<cstring>
#include<set>
using namespace std;
int main(){
int n,m;
char f[10];
cin>>n;
multiset<int> st,ps;
multiset<int>::iterator x;
for(int p=0;p<n;p++){
cin>>f>>m;
if(strcmp(f,"add")==0){
int j=0;
st.insert(m);
ps.insert(m);
for(x=st.lower_bound(m);x!=st.upper_bound(m);x++,j++);
cout<<j<<endl;
}
else if(strcmp(f,"del")==0){
int j=0;
for(x=st.lower_bound(m);x!=st.upper_bound(m);x++,j++);
cout<<j<<endl;
while(st.find(m)!=st.end())
st.erase(x);
}
else if(strcmp(f,"ask")==0){
int j=0;
x=ps.find(m);
if(x!=ps.end())
cout<<1<<" ";
else if(x==ps.end())
cout<<0<<" ";
for(x=st.lower_bound(m);x!=st.upper_bound(m);x++,j++);
cout<<j<<endl;
}
}
return 0;
}
for(x=st.lower_bound(m);x!=st.upper_bound(m);x++,j++);
你这句for循环是用来做什么的?为什么后面加个分号?只是循环什么也不干?
如果我的回答对你有帮助,请点击采纳按钮,谢谢
#include <iostream>
#include <set>
#include <string>
#include <iterator>
using namespace std;
int main()
{
multiset<int> mset;
set<int> mm;
char commend[5];
int i,n,num,amount;
multiset<int>::iterator it;
cin >> n;
for (i=0; i<n; i++)
{
cin >> commend >> num;
switch(commend[1])
{
case 'd':
mset.insert(num);
mm.insert(num);
cout << mset.count(num) << endl;
break;
case 'e':
cout << mset.count(num) << endl;
mset.erase(num);
break;
case 's':
if (mm.find(num)==mm.end())
cout << "0 0" << endl;
else
{
cout << "1 ";
cout << mset.count(num) << endl;
}
break;
}
}
return 0;
}