标准模板库实现
学了标准模板库,就该用标准模板库解决问题
#include<iostream>
#include<string>
#include<stack>
using namespace std;
char judge(char c)
{
char cneed;
switch(c)
{
case '{':cneed='}';break;
case '[':cneed=']';break;
case '(':cneed=')';break;
}
return cneed;
}
int main()
{
stack<char> s;
string brackets;
cin>>brackets;
for(int i=0;i<brackets.size();i++)
{
if(s.empty())
{
s.push(brackets[i]);
continue;
}
judge(s.top())==brackets[i]?s.pop():s.push(brackets[i]);
}
s.empty()? cout<<"匹配":cout<<"不匹配";
return 0;
}
还有更好的写法吗?