【发布时间】:2013-11-10 19:17:07
【问题描述】:
我的哈希表有二维向量
std::vector<std::vector<std::string> > htable;
和迭代器的类。
class myiterator{
public:
myiterator();
myiterator(std::vector<std::vector<std::string> >& v, int ii, int jj) :
vec(v), i(ii), j(jj) {}
myiterator& operator++(); // prefix operator
myiterator& operator--(); // prefix 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() {
int start=0;
while(htable[start].size()==0){
start++;
}
return (myiterator(htable, start, 0));
}
myiterator end(){
int end=htable.size()-1;
while(htable[end].size()==0){
end--;
}
return (myiterator(htable, end, htable[end].size()-1));
}
我已经为迭代器实现了开始和结束,但我不知道如何以及如何处理前缀运算符。另外,我无法用谷歌搜索运算符“->”是什么? 所以,拜托,你能给我一些关于二维向量实现前缀迭代器的小技巧或文章吗?提前致谢。
【问题讨论】:
-
以下是在 C++ 中重载前置/后置运算符的方法:stackoverflow.com/questions/894804/…