C++ | Leetcode C++题解之第284题窥视迭代器

题目:

题解:

cpp 复制代码
template <class T>
class PeekingIterator : public Iterator<T> {
public:
    PeekingIterator(const vector<T>& nums) : Iterator<T>(nums) {
        flag = Iterator<T>::hasNext();
        if (flag) {
            nextElement = Iterator<T>::next();
        }
    }
    
    T peek() {
        return nextElement;
    }

    T next() {
        T ret = nextElement;
        flag = Iterator<T>::hasNext();
        if (flag) {
            nextElement = Iterator<T>::next();
        }
        return ret;
    }
    
    bool hasNext() const {
        return flag;
    }
private:
    T nextElement;
    bool flag;
};
相关推荐
旖旎夜光18 小时前
C++(17)
c++·学习
Larry_Yanan19 小时前
Qt多进程(三)QLocalSocket
开发语言·c++·qt·ui
superman超哥19 小时前
仓颉语言中元组的使用:深度剖析与工程实践
c语言·开发语言·c++·python·仓颉
LYFlied19 小时前
【每日算法】LeetCode 153. 寻找旋转排序数组中的最小值
数据结构·算法·leetcode·面试·职场和发展
Lucas5555555520 小时前
现代C++四十不惑:AI时代系统软件的基石与新征程
开发语言·c++·人工智能
_MyFavorite_21 小时前
cl报错+安装 Microsoft Visual C++ Build Tools
开发语言·c++·microsoft
charlie11451419121 小时前
现代嵌入式C++教程:C++98——从C向C++的演化(2)
c语言·开发语言·c++·学习·嵌入式·教程·现代c++
zmzb010321 小时前
C++课后习题训练记录Day55
开发语言·c++
李白同学21 小时前
C++:继承
开发语言·c++
k***921621 小时前
【C++】STL详解(九)—priority_queue的使用与模拟实现
开发语言·c++