bool match(string str)
{
stack<int> con;
int count = 0;
string::iterator iter = str.begin();
while (iter != str.end())
{
if (*iter == '(')
con.push(*iter);
else if (*iter == ')')
{
if (con.empty())
return 0;
else
{
count++;
con.pop();
}
}
iter++;
}
if (con.empty())
{
cout << "匹配次数:" << count << endl;
return 1;
}
else
{
return 0;
}
}
测试结果: