我遇到了一個奇怪的問題。我認為這可能是由于 Mac 出了問題,但我不確定。本質上,我在我的 Windows 筆記本電腦上解決了這個 Leetcode 問題,并將代碼推送到我的 github 存盤庫。然后我稍后在我的 Mac 上獲取了該代碼,當我嘗試初始化任何 std:: 資料型別(如映射、向量或堆疊)時突然出現此錯誤。這很奇怪,因為我之前從未收到過這個錯誤,并且使用這些類到目前為止作業得很好。我不確定發生了什么,有人可以指出我如何解決這個問題的正確方向嗎?我在下面附上了顯示錯誤的螢屏截圖。我一直在環顧四周,找不到其他有類似問題的人。謝謝!
#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;
}
這些是我收到的錯誤(在旁邊有錯誤注釋的兩行):
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)
uj5u.com熱心網友回復:
使用-std=c 11(或更高)。
您正在嘗試使用std::initializer_listC 11 中引入的建構式。有關詳細資訊,請參閱矢量和地圖的檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/436417.html
