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;
};
相关推荐
OneQ6664 小时前
C++讲解---创建日期类
开发语言·c++·算法
JoJo_Way4 小时前
LeetCode三数之和-js题解
javascript·算法·leetcode
Coding小公仔6 小时前
C++ bitset 模板类
开发语言·c++
凌肖战6 小时前
力扣网C语言编程题:在数组中查找目标值位置之二分查找法
c语言·算法·leetcode
菜鸟看点7 小时前
自定义Cereal XML输出容器节点
c++·qt
悲伤小伞8 小时前
linux_git的使用
linux·c语言·c++·git
ysa0510308 小时前
数论基础知识和模板
数据结构·c++·笔记·算法
GEEK零零七8 小时前
Leetcode 1103. 分糖果 II
数学·算法·leetcode·等差数列
重庆小透明10 小时前
力扣刷题记录【1】146.LRU缓存
java·后端·学习·算法·leetcode·缓存
desssq10 小时前
力扣:70. 爬楼梯
算法·leetcode·职场和发展