···
/*
1(60分)
題目:一個(gè)整數(shù)集 , 該整數(shù)集可以有重復(fù)元素, 初始為空集合 。可以指定如下指令:
- add x 將x加入整數(shù)集
- del x 刪除整數(shù)集中所有與x相等的元素
- ask x 對(duì)整數(shù)集中元素x的情況詢(xún)問(wèn)
下面為指令解釋?zhuān)⒁筮M(jìn)行如下輸出: - add 輸出操作后集合中x的個(gè)數(shù)
- del 輸出操作前集合中x的個(gè)數(shù)
- ask 先輸出0或1表示x是否曾被加入集合(0表示不曾加入),再輸出當(dāng)前集合中x的個(gè)數(shù),中間用空格格開(kāi)。
提示
請(qǐng)使用STL中的 set 和multiset 來(lái)完成該題
輸入
第一行是一個(gè)整數(shù)n,表示命令數(shù)。0<=n<=100000。
后面n行命令。
輸出
共n行,每行按要求輸出。
樣例輸入
7 //第一行是整數(shù),表示為命令數(shù)目
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 <string>
include <iterator>
include<ctime>
include<cstdlib>
include<array>
include<vector>
include<algorithm>
using namespace std;
/*
在VS 2012環(huán)境中編譯通過(guò) Win7 X64
樣例輸入在工程文件夾下面 STL_1.in 文件中
輸出結(jié)果在工程文件夾下面 STL_1.out 文件中
如有問(wèn)題請(qǐng)直接在群里 :@北京 - amberfjx
*/
define COMAND_LINE
int main()
{
ifdef COMAND_LINE
freopen("STL_1.in","rt",stdin);
freopen("STL_1.out","wt",stdout);
endif
ifndef COMAND_LINE
cout<<"請(qǐng)輸入測(cè)試用例:\n"<<endl;
endif
multiset<int> mset;
set<int> mm;
char command[5];
int i,n,num,amount;
multiset<int>::iterator it;
cin >> n;
for (i = 0; i < n; i++)
{
cin >> command >> num;
switch(command[1])//這里取command[1] 而不是command[0] 是為了區(qū)分 {ask del add} 中的 add 和 ask 命令
{
case 'd': //add
mset.insert(num);
mm.insert(num);
cout << mset.count(num) << endl;
break;
case 'e': //del
cout << mset.count(num) << endl;
mset.erase(num);
break;
case 's': //ask
if (mm.find(num) == mm.end())
cout << "0 0" << endl;
else
{
cout << "1 ";
cout << mset.count(num) << endl;
}
break;
}
}
return 0;
}
···