Set代码

现有一整数集(允许有重复元素),初始为空。我们定义如下操作:
add x 把 x 加入集合
del x 把集合中所有与 x 相等的元素删除
ask x 对集合中元素x的情况询问

对每种操作,我们要求进行如下输出。
add 输出操作后集合中 x 的个数
del 输出操作前集合中 x 的个数
ask 先输出 0 或 1 表示 x 是否曾被加入集合(0表示不曾加入),再输出当前集合中 x 的个数,中间用空格格开。

输入格式

第一行是一个整数 n,表示命令数。0n100000。后面 nn 行命令,如 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<set>
#include<iterator>
using namespace std;

int main()
{
    int n;
    cin>>n;
    char command[4];
    multiset<int>mset;
    set<int>mm;
    multiset<int>::iterator it;
    int num;
    for (int i = 0; i < n; i++)
    {
        cin>>command>>num;
        switch (command[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':
        //find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
            if (mm.find(num)==mm.end())
            {
                cout<<"0 0"<<endl;
            }else
            {
                cout<<"1 "<<mset.count(num)<<endl;
            }
            break;
        default:
            break;
        }
    }
}

 

因上求缘,果上努力~~~~ 作者:每天卷学习,转载请注明原文链接:https://www.cnblogs.com/BlairGrowing/p/12709635.html

原文地址:https://www.cnblogs.com/BlairGrowing/p/12709635.html