I'm running into a weird problem. I'm thinking this could be possibly due to something wrong with Mac, but I'm not sure. Essentially, I solved this Leetcode problem on my windows laptop and pushed the code to my github repo. Then I fetched that code on my mac later on and all the sudden I get this error when trying to initialize any std:: data types such as a map, vector, or stack. It's weird because I was never receiving this error before and using those classes worked perfectly up to this point. I'm not really sure what's up, could someone point me in the right direction as to how to fix this? I've attached a screenshot below showing the error. I have been looking around and I can't find anyone else with a similar problem. Thanks!
#include <vector>
#include <map>
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdlib.h>
using namespace std;
/**
* @brief Rules:
* 1. Open brackets must be closed by the same type of brackets.
* 2. Open brackets must be closed in the correct order.
*
* Leetcode Challenge can be found here: https://leetcode.com/problems/valid-parentheses/
*/
//correlate the opening character with the closing character.
map<char, char> legend {{'{','}'},{'(',')'},{'[',']'}};//Error
vector<int> vect{0,2,3,4,5};//Error
bool isValid(string s)
{
//if s is odd then we know a bracket hasn't been closed. Return false.
if (s.length() % 2 != 0)
{
return false;
}
vector<char> stack; //initiate our stack where we'll store opening characters "(, {, ["
bool stackIsModified = false; //We need to make sure an opening character has been added to the stack at least once.
for (int i = 0; i < s.length(); i )
{
if (s[i] == '{' || s[i] == '[' || s[i] == '(')//Check and see if s[i] is an opening character.
{
stackIsModified = true;
stack.push_back(s[i]);
cout << s[i] << endl;
}
else if(stack.size() != 0) //See if s[i] is a closing character.
{
if (legend[stack.at(stack.size() - 1)] == s[i])
{
stack.pop_back();
}
else //If s[i] is a closing character that doesn't match the corresponding opening character. Ex: "{)"
{
return false;
}
}
else //If s[i] isn't an opening character and the stack is empty then we know there's a mismatch, return false.
{
return false;
}
}
if (stack.size() > 0 || !stackIsModified) //Make sure the stack doesn't have remaining opening characters and that the stack has been changed at least once.
{
cout << stackIsModified << endl;
return false;
}
return true;
}
int main()
{
cout << isValid("()))") << endl;//Random test case.
return 0;
}
And these are the errors I'm recieving (at the two lines that have error commented next to them):
no instance of constructor "std::__1::map<_Key, _Tp, _Compare, _Allocator>::map [with _Key=char, _Tp=char, _Compare=std::__1::less<char>, _Allocator=std::__1::allocator<std::__1::pair<const char, char>>]" matches the argument listC/C (289)
std::__1::vector<int> vect
no instance of constructor "std::__1::vector<_Tp, _Allocator>::vector [with _Tp=int, _Allocator=std::__1::allocator<int>]" matches the argument listC/C (289)
CodePudding user response:
Use -std=c 11
(or higher).
You are trying to use constructors from std::initializer_list
that were introduced in C 11.
For further information, see the documentation for vectors and maps.