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;
};
相关推荐
豪斯有话说16 分钟前
C++_哈希表
数据结构·c++·散列表
编程绿豆侠1 小时前
力扣HOT100之栈:394. 字符串解码
java·算法·leetcode
real_metrix1 小时前
【学习笔记】erase 删除顺序迭代器后迭代器失效的解决方案
c++·迭代器·迭代器失效·erase
朝朝又沐沐1 小时前
基于算法竞赛的c++编程(18)string类细节问题
开发语言·c++·算法
记得早睡~2 小时前
leetcode73-矩阵置零
数据结构·leetcode·矩阵
爱coding的橙子2 小时前
每日算法刷题Day27 6.9:leetcode二分答案2道题,用时1h20min
算法·leetcode·职场和发展
a.3022 小时前
C++ 时间处理指南:深入剖析<ctime>库
数据结构·c++·算法
Dave_Young3 小时前
上位机开发过程中的设计模式体会(1):工厂方法模式、单例模式和生成器模式
c++·设计模式
old_power4 小时前
在 Windows 系统下配置 VSCode + CMake + Ninja 进行 C++ 或 Qt 开发
c++·windows·vscode·cmake·ninja
UP_Continue4 小时前
C++--string的模拟实现
java·开发语言·c++