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;
};
相关推荐
-dzk-7 小时前
【代码随想录】LC 59.螺旋矩阵 II
c++·线性代数·算法·矩阵·模拟
m0_706653238 小时前
C++编译期数组操作
开发语言·c++·算法
qq_423233909 小时前
C++与Python混合编程实战
开发语言·c++·算法
TracyCoder1239 小时前
LeetCode Hot100(19/100)——206. 反转链表
算法·leetcode
m0_715575349 小时前
分布式任务调度系统
开发语言·c++·算法
CSDN_RTKLIB9 小时前
简化版unique_ptr说明其本质
c++
naruto_lnq9 小时前
泛型编程与STL设计思想
开发语言·c++·算法
踩坑记录9 小时前
leetcode hot100 94. 二叉树的中序遍历 easy 递归 dfs
leetcode
m0_7487080510 小时前
C++中的观察者模式实战
开发语言·c++·算法
时光找茬10 小时前
【瑞萨AI挑战赛-FPB-RA6E2】+ 从零开始:FPB-RA6E2 开箱测评与 e2 studio 环境配置
c++·单片机·边缘计算