在我的声明中,我列出了如下列表
list<string> abc;
我已经保存了一些东西。
现在我想进行关键字搜索。
int counter = 0;
list<string>::iterator it;
key = "something"
for (it = abc.begin(); it != abc.end(); it++){
if(*it.find(key) != std::string::npos)
counter++;
}
现在它给出了一个错误“没有名为'find'的成员在std :: _ 1 :: _ list_iterator,void *&gt;';你的意思是使用 - &gt;而不是。? “
所以我改变了if(*it->find(key) != std::string::npos)
它也给出了一个错误
“间接需要指针操作数('size_type'(又名'unsigned long')无效)”
有谁知道这是什么问题?
此外,我也尝试过cout列表的类型
“cout << type of (*it)
”
但它也出现了错误...
答案 0 :(得分:1)
您希望首先取消引用迭代器,然后调用find
:
if(it->find(key) != std::string::npos)
相当于:
if((*it).find(key) != std::string::npos)
您首先编写的内容*it.find(key)
将由编译器解析为*(it.find(key))
,即在find
上首先调用it
,然后取消引用结果。
之后您尝试的内容*it->find(key)
将被解析为*(it->find(key))
,即首先取消引用it
,然后在结果上调用find
,最后取消引用find
的结果。
他们都没有做你想要的。
要记住:
a->b
= (*a).b
*a.b
= *(a.b)