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;
};
相关推荐
草莓熊Lotso9 分钟前
Linux 文件描述符与重定向实战:从原理到 minishell 实现
android·linux·运维·服务器·数据库·c++·人工智能
历程里程碑12 分钟前
Linux22 文件系统
linux·运维·c语言·开发语言·数据结构·c++·算法
YGGP1 小时前
【Golang】LeetCode 128. 最长连续序列
leetcode
在路上看风景7 小时前
19. 成员初始化列表和初始化对象
c++
zmzb01038 小时前
C++课后习题训练记录Day98
开发语言·c++
念风零壹8 小时前
C++ 内存避坑指南:如何用移动语义和智能指针解决“深拷贝”与“内存泄漏”
c++
孞㐑¥9 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
月挽清风9 小时前
代码随想录第十五天
数据结构·算法·leetcode
MZ_ZXD00111 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
TracyCoder12311 小时前
LeetCode Hot100(34/100)——98. 验证二叉搜索树
算法·leetcode