284. 顶端迭代器

 1 /*
 2  * Below is the interface for Iterator, which is already defined for you.
 3  * **DO NOT** modify the interface for Iterator.
 4  *
 5  *  class Iterator {
 6  *        struct Data;
 7  *         Data* data;
 8  *        Iterator(const vector<int>& nums);
 9  *         Iterator(const Iterator& iter);
10  *
11  *         // Returns the next element in the iteration.
12  *        int next();
13  *
14  *        // Returns true if the iteration has more elements.
15  *        bool hasNext() const;
16  *    };
17  */
18 
19 class PeekingIterator : public Iterator 
20 {
21 public:
22     PeekingIterator(const vector<int>& nums) : Iterator(nums) 
23     {
24         // Initialize any member here.
25         // **DO NOT** save a copy of nums and manipulate it directly.
26         // You should only use the Iterator interface methods.
27         
28     }
29     
30     // Returns the next element in the iteration without advancing the iterator.
31     int peek() 
32     {
33         auto temp = *this;
34         return temp.next();
35     }
36     
37     // hasNext() and next() should behave the same as in the Iterator interface.
38     // Override them if needed.
39     int next() 
40     {
41         return Iterator::next();
42     }
43     
44     bool hasNext() const 
45     {
46         return Iterator::hasNext();
47     }
48 };

 

posted @ 2020-04-15 13:57  Jinxiaobo0509  阅读(96)  评论(0)    收藏  举报