问题描述
我对c ++有点陌生,所以这个问题可能没有任何意义,因此,对此先感到抱歉.所以,我有一类哈希表,我的哈希表是向量的向量,这意味着我使用了
I am a bit new at c++, so the question may be without any meaning, so, sorry about that in advance.So, i have a class of hashtable, my hashtable is vector of vectors, it means that i used
std::vector<std::vector<std::string> > htable;
我的任务是-使用操作++,-,->和*创建自己的迭代器.我写了这个
My task is - create own iterator with operation ++, --, -> and *. I wrote this
class hashTable
{
public:
hashTable(int size);
~hashTable();
void add(std::string s);
bool inHash(std::string s);
void deletestr(std::string s);
void printall();
int maxcoll();
private:
std::vector<std::vector<std::string> > htable;
std::vector<std::vector<std::string> > newhtable;
int hfunc(std::string s);
void reallocate();
int M;
int teksize;
int issame(std::string a, std::string b);
class myiterator{
myiterator();
myiterator operator*();
myiterator operator++();
myiterator operator--();
myiterator operator->();
};
myiterator begin() {return htable.begin()}
myiterator end() {return htable.end()}
};
我认为我理解了interator是什么,但是现在我想我是错的,因此当我尝试编译它时,在行中会出现错误
I think, that i understand what the interator is, but now i suppose that i was wrong, so when i try to compile this, there is a mistake in lines
myiterator begin() {return htable.begin()}
myiterator end() {return htable.end()}
/Users/ratkke/Programms/c ++/mipt/tasks/#5/myhash.cpp:37:29:错误:没有从迭代器"(又名"__wrap_iter")到"hashTable :: myiterator"的可行转换 myiterator begin(){返回htable.begin()} ^ ~~~~~~~~~~~~~/Users/ratkke/Programms/c++/mipt/tasks/#5/myhash.cpp:29:8:注意:候选构造函数(隐式副本构造函数)不可行:没有来自迭代器"(又称"__wrap_iter")的转换到"const hashTable :: myiterator&"对于第一个参数 类迭代器{
/Users/ratkke/Programms/c++/mipt/tasks/#5/myhash.cpp:37:29: error: no viable conversion from 'iterator' (aka '__wrap_iter') to 'hashTable::myiterator' myiterator begin() {return htable.begin()} ^~~~~~~~~~~~~~/Users/ratkke/Programms/c++/mipt/tasks/#5/myhash.cpp:29:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'iterator' (aka '__wrap_iter') to 'const hashTable::myiterator &' for 1st argument class myiterator{
我不知道为什么.另外,您能告诉我关于向量迭代器的迭代器实现的问题(或只是链接到文章),因为我不明白如何实现所有这些运算符.先感谢您.
And i don't know why. Also, can you tell me about realisation of iterator(or just link to article) for iterator for vectors, because i can't understand how i must implement all this operators.Thank you in advance.
推荐答案
您需要实现myiterator
.有很多方法可以做到这一点,但至少您必须在myiterator
中添加一些内容.例如
You need to implement your myiterator
. There are lots of way to do that, but at very least you must add something to myiterator
. For instance
class myiterator{
public:
myiterator();
myiterator(std::vector<std::vector<std::string> >& v, int ii, int jj) :
vec(v), i(ii), j(jj) {}
std::string operator*();
myiterator& operator++(); // prefix operator
myiterator operator++(int); // postfix operator
myiterator& operator--(); // prefix operator
myiterator operator--(int); // postfix operator
std::string* operator->();
private:
std::vector<std::vector<std::string> >& vec; // the vector we are iterating over
int i; // the position in the vector (first dimension)
int j; // the position in the vector (second dimension)
};
myiterator begin() {return myiterator(htable, 0, 0);}
很多其他方法可以实现,而上面的方法可能并不完全是您想要的,但是希望这可以给您一个主意,myiterator
必须有一些数据成员来保存要迭代的向量并达到位置远.
Lots of other ways to do it, and the above may not be exactly what you want, but hopefully this gives you the idea, myiterator
must have some data members that hold the vector being iterated over and the position reached so far.
还请注意,operator*
的返回类型是错误的,(应该)应该是std::string
.其他一些运算符也不正确,我已经在代码中添加了我认为正确的内容.
Also note the return type of operator*
is wrong, it should (presumably) be std::string
. Some of the other operators don't look right either, I've put what I think is correct in the code.
这篇关于为双向量创建自己的迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!