每日一题4.15.1

每日一题4.15.1

数据库连接池

在这里插入图片描述
在这里插入图片描述
解题思路: 使用一个set来做,如果某个记录是connect,就加到set中去,如果是disconnect,就把set中对应的值删 除掉。在加进去的过程中,不断判断set元素的最大个数,最终返回这个最大个数即可。
代码实现:

#include<string>
#include<set>
using namespace std;
int main()
{
	int n;
	while (cin >> n)
	{
		set<string> pool;
		string id, condition;
		int maxsize = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> id >> condition;
			if (condition == "connect")
				pool.insert(id);
			else if (condition == "disconnect")
				pool.erase(id);
			int size = pool.size();
			maxsize = max(maxsize, size);
		}
		cout << maxsize << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lxb18821659801/article/details/89515372