251. Flatten 2D Vector
class Vector2D {
public:
Vector2D(vector<vector<int>>& vec2d) {
rowBegin = vec2d.begin();
rowEnd = vec2d.end();
if (rowBegin != rowEnd) colBegin = rowBegin->begin();
}
int next() {
hasNext();
return *(colBegin++);
}
bool hasNext() {
while (rowBegin != rowEnd && colBegin == rowBegin->end()) {
rowBegin++;
colBegin = rowBegin->begin();
}
return rowBegin != rowEnd;
}
private:
vector<vector<int>>::iterator rowBegin, rowEnd;
vector<int>::iterator colBegin;
};
/**
* Your Vector2D object will be instantiated and called as such:
* Vector2D i(vec2d);
* while (i.hasNext()) cout << i.next();
*/