我必须编写一个接受字符串并返回布尔值的函数。传入的字符串是一系列或不同的括号打开或关闭 ex.({[]}) 并返回“parens”是否平衡。这必须通过将项目添加到堆栈来实现。我收到以下错误:
parenMatching_demo.cpp:18:12: 错误: 'c == '(' 中的 'operator==' 不匹配
伪代码是:
matcher(expression)
for each character in expression
if the character is an opener
push on stack
else if the character is a closr
if stack is empty return false
if the (opener at) the top of the stack does not match the closer
return false
pop the stack
if the stack is not empty return false
return true
这就是我所拥有的。
template <typename T>
bool parenMatching(string c) {
stack<string> s;
for (int i = 0; i < s.size(); i++) {
if (c == '(' || c == '[' || c == '{' || c == '<')
s.push(c);
else if (c == ')' || c == ']' || c == '}' || c == '>') {
if (s.empty()) return false;
if (s.top() != '(' || s.top() != '[' || s.top() != '{' || s.top() != '<')
return false;
s.pop();
}
}
}