c ++ Programming Exercises 041: Set

North programming and algorithms (c) quiz summary (Spring 2020)


description

A conventional set of integers (allowing duplicate elements), initially empty. We define as follows:
the Add x x added to collection
del set x to x equal to all the element deletion
ASK x in a case where the collection element x interrogation
of each operation, we require the following output.
set x add output operation after the number of
the number x set before the output operation del
ask first output indicating whether 0 or 1 was added to the set of x (0 indicates never added), and then outputs the number of the current set of x, intermediate opening by spaces.

Input
of the first line is an integer n, the number of commands. 0 <= n <= 100000.
N back line commands, as described in the Description.

Output
total n lines, each line of output required.

Sample input
. 7
the Add. 1
the Add. 1
ASK. 1
ASK 2
del 2
del. 1
ASK. 1

Sample output
. 1
2
. 1 2
0 0
0
2
. 1 0

提示
Please use STL’s set and multiset to finish the task


analysis

Use the template set

#include <set>
#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
	multiset<int> sets;
	set<int> be_sets;
	int n;
	cin >> n;
	char cmd[20];
	int  num;
	set<int>::iterator li;
	while (n--) {
		cin >> cmd;
		int count = 0;
		switch (cmd[1]) {
		case 'd':
			cin >> num;
			sets.insert(num);
			be_sets.insert(num);
			cout << sets.count(num) << endl;
			break;
		case 'e':
			cin >> num;
			cout << sets.count(num) << endl;
			for (li = sets.begin(); li != sets.end(); li++){
				if (*li == num){
					sets.erase(li);
				}
			}
			break;
		case 's':
			cin >> num;
			if (be_sets.find(num) != be_sets.end()) {
				cout << 1 << " " << sets.count(num) << endl;
			}
			else
				cout << 0 << " " << sets.count(num) << endl;
			break;
		}
	}
	return 0;
}
Published 222 original articles · won praise 48 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_44116998/article/details/104419595