【发布时间】:2015-07-12 13:39:29
【问题描述】:
我不知道为什么会出现非法间接错误:
#include <iostream>
#include <algorithm>
#include <stack>
#include <unordered_map>
using namespace std;
int main()
{
string s1 = "";
stack<char> s;
unordered_map<char, char> m;
m.insert('(', ')');
m.insert('{', '}');
m.insert('[', ']');
for (auto x : s1)
{
if (m.find(x) != m.end()) s.push(x);
else
{
auto it = m.find(s.top());
if (s.empty() || (it->second) != x) { cout << "Invalid\n"; s.pop(); break; }
}
}
}
它是映射条目的迭代器,我正在尝试访问它的值。
【问题讨论】:
-
如果
s1包含不以括号开头的内容,这将导致未定义的行为,因为您在空堆栈上调用s.top()
标签: c++ dictionary stl iterator