I am trying to get hands on in C++ and have written this below piece of code--
// 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;
}
However, when I tried to compile this code I ran into a lot of compilation errors like 'C2039'cout': is not a member of 'std', C2065 'string': undeclared identifier etc. I was able to get the code compile by moving the header #include "stdafx.h" to the top. So I wanted to understand deeper, how does changing the order of the header file could get my code compile successfully.