OJ:多重集的插入和删除

来源:互联网 发布:影子网络怎么进入 编辑:程序博客网 时间:2024/04/30 15:13
Description给你一个集合,一开始集合是空集,然后进行若干操作,最后你要从小到大输出集合中的元素,以空格隔开。(集合中可能会有相同元素)Input一共有若干输入数据,开头一个n(n<=20),n=0代表输入结束。然后有n行,每行有2种形式:“i x”,x是一个整数,代表向集合中插入元素x“d x”,x是一个整数,代表删除一个xOutput每组输入结束后,从小到大输出集合中的元素,以空格隔开。Sample Input2 i 2 i 2 4 i 1 i 1 i 2 d 1 0Sample Output2 2 1 2
#include <iostream>
#include <set>using namespace std;int main(){    int n,x,y,m;    char c;    multiset<int> a;    multiset<int>::iterator p;    while(cin>>n)    {        m=0;        if(n==0)            return 0;        for(int i=0;i<n;i++)        {            cin>>c;            if(c=='i')            {                cin>>x;                a.insert(x);            }            if(c=='d')            {                cin>>y;                m++;            }       }        if(!a.empty())        {            if(m!=0)            {                while(m--)                {                   for(p=a.begin();p!=a.end();++p)                    {                        if(*p==y)                        {a.erase(p);                            break;}                    }                }            }        }        if(!a.empty())        {            for(p=a.begin();p!=a.end();++p)            {                if(p==a.begin())                    cout<<*p;                else                    cout<<" "<<*p;            }            cout<<endl;        }     a.clear();    }}





0 0