描述
现有一整数集(允许有重复元素),初始为空。我们定义如下操作:
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
这里按照自己的输出格式给出如下代码:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <functional>
using namespace std;
template <class T>
struct print
{
void operator()(const T &x)
{
cout << x << endl;
}
};
int main()
{
int n;
cin >> n;
multiset<int> mySet;
for (int i = 0; i < n; i++)
{
string str;
cin >> str;
if (str == "add")
{
int tmp;
cin >> tmp;
mySet.insert(tmp);
cout << "添加后当前集合大小:"<<mySet.size() << endl;
}
else if (str == "ask")
{
int tmp;
cin >> tmp;
int num = count(mySet.begin(), mySet.end(), tmp);
if (num > 0)
cout << "该数子个数为:" << num << endl;
else
cout << "不存在:0" << endl;
}
else if (str == "del")
{
int tmp;
cin >> tmp;
mySet.erase(tmp); //删除所有的tmp值
cout << "删除后当前集合大小:" << mySet.size() << endl;
}
else
cout << "Please check the cmd !" << endl;
}
return 0;
}