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;
};
相关推荐
cynicme1 小时前
力扣3318——计算子数组的 x-sum I(偷懒版)
java·算法·leetcode
ShineSpark3 小时前
Crashpad 在windows下编译和使用指南
c++·windows
Larry_Yanan4 小时前
QML学习笔记(五十)QML与C++交互:QML中单例C++对象
开发语言·c++·笔记·qt·学习·ui·交互
im_AMBER4 小时前
算法笔记 09
c语言·数据结构·c++·笔记·学习·算法·排序算法
SweetCode5 小时前
C++ 实现大数加法
开发语言·c++·算法
stay_alive.6 小时前
C++ 四种类型转换
开发语言·c++
卡提西亚6 小时前
C++笔记-9-三目运算符和switch语句
c++·笔记
CodeWizard~6 小时前
AtCoder Beginner Contest 430赛后补题
c++·算法·图论
喜欢吃燃面6 小时前
C++:哈希表
开发语言·c++·学习
mit6.8246 小时前
[C++] 时间处理库函数 | `tm`、`mktime` 和 `localtime`
开发语言·c++