c++ - 为什么这段代码编译失败?
问题描述
我正在尝试使用 C++ 并在下面写了这段代码-
// 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" 移动到顶部。所以我想更深入地了解,改变头文件的顺序是如何让我的代码编译成功的。
解决方案
推荐阅读
- php - 从隐藏在其他数组中的特定节点中获取特定节点
- angular - 使用基于属性值的类
- linkedin - 使用linkedin的Auth0登录获取公共个人资料URL
- ruby-on-rails - 如何构建具有动态范围的 has_many 关联?
- python - 如何将具有 ResNet 等非序列架构的 Keras 模型拆分为子模型?
- c# - Unity - 我如何将铅笔移动到高于鼠标的位置
- python - 连接到 mongo 数据库,给定 ssh 密钥
- sql - SQL - 使用 AND 与使用子查询的 INNER JOIN
- android - android启动时如何setenforce 0
- compiler-optimization - GraalVM:如何实现编译器优化?