Creating own iterator for double vectors

后端 未结 1 1542
暗喜
暗喜 2021-01-28 04:38

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

相关标签:
1条回答
  • 2021-01-28 05:06

    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);}
    

    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.

    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.

    0 讨论(0)
提交回复
热议问题