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

来源:互联网 发布:上海华讯网络上市了吗 编辑:程序博客网 时间:2024/05/21 09:39
HomeWeb BoardProblemSetStandingStatusStatistics

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

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1822  Solved: 1336
[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]
#include <iostream>#include <set>#include <iterator>using namespace std;int main(){    int n;    while(cin >> n && n)    {        multiset <int, less<int> > mul;        multiset <int, less<int> > :: iterator  p;        for(int i = 0; i < n; i++)        {            char order;            int elem;            cin >> order >> elem;            if(order == 'i')                mul.insert(elem);            if(order == 'd')            {                p = mul.find(elem);                mul.erase(p);            }        }        p = mul.begin();        cout << *p;        p++;        for(; p != mul.end(); p++)           cout << " " << *p;           cout << endl;    }}

0 0
原创粉丝点击