【发布时间】:2019-07-02 08:50:47
【问题描述】:
我创建了一个 Map,其键为字符串类型,关联的值存储在向量中。现在我有一个字符串,需要检查字符串中的每个字符是否作为映射中的键存在。
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <map>
#include <string>
using namespace std;
int main() {
map<string, vector<string>> umap;
umap["1"] = {"a","b","c"};
umap["2"] = {"d","e","f"};
string s = "23";
for(int i=0; i<s.length(); i++) {
if(umap.find(s[i]) != umap.end())
cout<<"Present"<<endl;
else
cout<<"Not Present"<<endl;
}
}
错误:
main.cpp: In function ‘int main()’:
main.cpp:15:26: error: no matching function for call to ‘std::map<std::__cxx11::basic_string<char>, std::vector<std::__cxx11::basic_string<char> > >::find(__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type&)’
if(umap.find(s[i]) != umap.end())
【问题讨论】:
-
s[i]不是std::string,而是char。 -
试试
umap.find(s.substr(i, 1))...