0284.cpp (525B)
1 class PeekingIterator : public Iterator { 2 int l_next = -1; 3 bool has_next = false; 4 5 public: 6 PeekingIterator(const vector<int> &nums) : Iterator(nums) { 7 if (!Iterator::hasNext()) return; 8 l_next = Iterator::next(); 9 has_next = true; 10 } 11 12 int peek() { return l_next; } 13 14 int next() { 15 const int crnt = l_next; 16 has_next = Iterator::hasNext(); 17 if (has_next) l_next = Iterator::next(); 18 return crnt; 19 } 20 21 bool hasNext() const { return has_next; } 22 };