[英]vector of lists + iterator CPP
I am trying to implement insertion of a word into a chained hashtable. 我正在尝试实现将单词插入链式哈希表中。
The problem is I am trying to insert a object that has 2 fileds and I need access to one with an iterator. 问题是我试图插入一个具有2个字段的对象,并且需要使用迭代器访问一个对象。 The problem seems to happen with the iterator
it
as the code doesn't work from the for cycle. 该问题似乎发生在迭代器上
it
因为代码在for循环中不起作用。 I also overloaded the operator==
in Vocabolo.cpp
to make it work for my case. 我还使
Vocabolo.cpp
的operator==
重载,以使其适用于我的情况。
I also have a problem the size of the vector, can I use a define? 我也有向量大小的问题,可以使用定义吗? It seems not.
好像没有 Any advices please?
有什么建议吗?
I declared my vector of list + iterator in the header file as : 我在头文件中将list +迭代器的向量声明为:
vector<list<Vocabolo>> hash;
list<Vocabolo>::iterator it;
this is part of the class Vocabolo : 这是Vocabolo类的一部分:
class Vocabolo {
public:
Vocabolo();
~Vocabolo();
void setVocabolo(Vocabolo);
string getVocabolo();
bool operator== (Vocabolo);
string termine;
string tipo;
};
this is the overloaded method operator==: 这是重载的方法operator ==:
bool Vocabolo::operator== (Vocabolo x) {
return getVocabolo() == x.termine;
}
the method that is not working! 该方法不起作用!
bool HashV::Insert(Vocabolo nuovo) {
key = this->HashUniversale(nuovo.termine);
for (it = this->hash[key].begin(); it != this->hash[key].end(); it++)
if (it->termine == nuovo.termine)
return false;
else {
hash[key].push_back(nuovo);
return true;
}
}
Consider using std::find_if instead: 考虑改用std :: find_if:
auto itVoca = std::find_if(this->hash[key].begin(), this->hash[key].end(), [nuovo](const string& str)
{
return str != nuovo.termine;
});
bool found = itVoca != this->hash[key].end();
if(found ) hash[key].push_back(nuovo);
return found;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.