Why not add the begin () and end () function to the class, but simply return v.begin(); and return v.end(); ? (Assuming v is your vector.)
class MyVectorWrapper { public: // Give yourself the freedom to change underlying types later on: typedef vector<int>::const_iterator const_iterator; // Since you only want to read, only provide the const versions of begin/end: const_iterator begin() const { return v.begin(); } const_iterator end() const { return v.end(); } private: // the underlying vector: vector<int> v; }
Then you can iterate through your class, like any other:
MyVectorWrapper myV; for (MyVectorWrapper::const_iterator iter = myV.begin(); iter != myV.end(); ++i) { // do something with iter: }
Bill source share