【发布时间】:2021-05-26 13:52:17
【问题描述】:
当我尝试对 map
错误
Line 20: Char 29: error: no matching member function for call to 'find'
auto it = b.find(s[i]);
~~^~~~
#include<map>
#include<vector>
#include<string>
class Solution {
public:
bool isValid(string s) {
int l = s.length();
map<string,string> b;
b["("] = ")";
b["{"] = "}";
b["["] = "]";
vector<string> br;
if(l%2 == 1)
return false;
else {
for (int i=0;i<l;i++){
auto it = b.find(s[i]); \\ line where error is pointed
if(it != b.end() && br.size()==0){
return false;
}
else if(it != b.end() && br.size()>0){
if(br.rbegin() == s[i]){
br.pop_back();
}
【问题讨论】:
-
s[i]是char。find()包含字符串的映射需要一个字符串 -
if(br.rbegin() == s[i])也不会按照你的想法去做,或者更确切地说它甚至不会编译。由于拼写错误(或多个拼写错误),我投票关闭。 -
与 Python 不同,其中
'a'和"a"是同一个东西,单个字符 ('a') 与 C++ 中的单字符字符串 ("a") 不同。看起来你实际上想要一个map<char, char>。
标签: c++ string algorithm dictionary find