【发布时间】:2021-11-27 13:16:06
【问题描述】:
我试图存储在字符串数组中重复的单词数...
int countWords(string list[], int n)
{
map <string, int> mp;
for(auto val = list.begin(); val!=list.end(); val++){
mp[*val]++;
}
int res = 0;
for(auto val= mp.begin(); val!=mp.end(); val++){
if(val->second == 2) res++;
}
return res;
}
但我收到如下错误:
prog.cpp: In member function int Solution::countWords(std::__cxx11::string*, int):
prog.cpp:14:32: error: request for member begin in list, which is of pointer type std::__cxx11::string* {aka std::__cxx11::basic_string<char>*} (maybe you meant to use -> ?)
for(auto val = list.begin(); val!=list.end(); val++){
^
prog.cpp:14:51: error: request for member end in list, which is of pointer type std::__cxx11::stri.................
请有人调查一下。
【问题讨论】:
-
你是如何调用函数的?
list只是一个指向std::string的指针。即使您使用了->,begin和end也很可能不是您想要的(因为这将是一个单一的std::string的迭代器)
标签: c++ arrays dictionary stl