问题描述
// BalancedStrings.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <stack>
#include "stdafx.h"
using namespace std;
bool isBalanced(string s) {
stack<char> stack;
char l;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '(' || s[i] == '{' || s[i] == '[') {
stack.push(s[i]);
continue;
}
if (stack.empty())
return false;
switch (s[i]) {
case ')':
l = stack.top();
stack.pop();
if (l == '{' || l == '[')
return false;
case '}':
l = stack.top();
stack.pop();
if (l == '(' || l == '[')
return false;
break;
case ']':
l = stack.top();
stack.pop();
if (l == '{' || l == '(')
return false;
break;
}
}
return true;
}
int main()
{
string s1 = "{}";
std::cout << isBalanced(s1);
return 0;
}
然而,当我试图编译这段代码时,我遇到了很多编译错误,比如 'C2039'cout': is not a member of 'std',C2065 'string': undeclared identifier 等。我能够得到通过将标题 #include "stdafx.h" 移动到顶部来编译代码。所以我想更深入地了解,如何改变头文件的顺序才能让我的代码编译成功。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)