标签:
class PeekingIterator : public Iterator { bool bPeeked; int val; public: PeekingIterator(const vector<int>& nums) : Iterator(nums) { val = -1; bPeeked = false; } // Returns the next element in the iteration without advancing the iterator. int peek() { if (!bPeeked) { val = Iterator::next(); bPeeked = true; } return val; } // hasNext() and next() should behave the same as in the Iterator interface. // Override them if needed. int next() { if (bPeeked) { bPeeked = false; return val; } return Iterator::next(); } bool hasNext() const { return bPeeked || Iterator::hasNext(); } };
标签:
原文地址:http://www.cnblogs.com/tonix/p/4826322.html