在c++中从列表中删除项
Removing item from list in c++
大家好,我正在为哈希表编写代码,我想在列表中删除一些字符串值的删除函数。
void Hash::remove(string word)
{
int i,flag=0;
list<string>::iterator it;
for(i=0;i<10;i++)
{
for(it=hashTable[i].begin();it!=hashTable[i].end();it++)
{
if(word==*it){
hashTable.erase(it);
break;
}
}
}
}
但是当我编译得到一个错误:错误:请求成员' erase '在' ((Hash*)this)->Hash::hashTable '中,它是非类类型' std::list> [10] '
我不能理解这个。
用hashTable[i].erase(it)
代替hashTable.erase(it)
从您的描述中,似乎您要删除与word
匹配的所有元素。不确定为什么要使用两个循环和一个break
语句。你可以使用Erase - Remove习语来有效地从容器中删除元素。
在c++ 11中,你可以尝试-
hashTable.erase( std::remove( std::begin(hashTable),
std::end(hashTable),
[word](const std::string& str){ return str == word;} ),
std::end(hashTable) );
相关文章:
- C++如何通过用户输入删除列表元素
- 删除列表中的第n个元素
- 删除列表 c++ 中的最后 3 个元素
- 链表.如何删除列表中的第一个节点?
- 在 c++ 中删除列表中的重复元素
- 如何删除列表中的动态分配类对象<T>?
- 无法在C 中删除列表元素
- 使用共享_ptr删除列表中的对象
- 删除列表向量中的元素并显示当前列表
- 如何在C++中删除列表中的某些元素
- 当删除列表元素时,在迭代器结束时会发生什么
- 删除列表中的对象后,为什么可以访问该对象的成员变量
- 如何删除列表中的字符串
- C++迭代时删除列表成员:标准解决方案不起作用
- 迭代时删除列表中的最后一个元素会导致错误
- 如何正确删除列表中的对象
- 在 c++ 中删除列表
- 当链表节点是用"new"构造时,如何删除列表?
- 从另一个 c++ 中删除 1 列表中出现的所有元素
- 删除列表 CPP 中的重复项