Problem H: STL——多重集的插入和删除

Problem H: STL——多重集的插入和删除

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 2464   Solved: 1670
[ Submit][ Status][ Web Board]

Description

给你一个集合,一开始集合是空集,然后进行若干操作,最后你要从小到大输出集合中的元素,以空格隔开。(集合中可能会有相同元素)

Input

一共有若干输入数据,开头一个n(n<=20),n=0代表输入结束。
然后有n行,每行有2种形式:
“i x”,x是一个整数,代表向集合中插入元素x
“d x”,x是一个整数,代表删除一个x

Output

每组输入结束后,从小到大输出集合中的元素,以空格隔开。

Sample Input

2i 2i 24i 1i 1i 2d 10

Sample Output

2 21 2

HINT

用STL的multiset容易解决


Append Code

[ Submit][ Status][ Web Board]
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while (cin>>n&&n&&n<=20){
       multiset< int >s;
       while (n--){
         char a; int m;
         cin>>a>>m;
         if (a== 'i' ){ s.insert(m);}
         if (a== 'd' ){
            multiset< int ,less< int > > ::iterator p;
            for (p=s.begin();p!=s.end();p++){
                 if (m==*p){ s.erase(p); break ;}
            }
         }
       }
       multiset< int ,less< int > > ::iterator it;
       for (it=s.begin();it!=s.end();it++){
          if (it==s.begin())
             cout<<*it;
          else
             cout<< " " <<*it;
       }
       cout<<endl;
 
    }
    return 0;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值