当前位置: 首页 > wzjs >正文

做游戏攻略网站赚钱吗wordpress免签

做游戏攻略网站赚钱吗,wordpress免签,怀柔做网站的吗,长春百度seo排名实现自己的迭代器 最近在写数据结构&#xff0c;使用类模板实现&#xff0c;碰到了一些问题&#xff0c;其中有一个就是遍历的问题&#xff0c;查阅资料最后实现了自己的迭代器&#xff0c;让我实现的数据结构能像STL一样进行for循环遍历。 类的构成 #include <stdexcept…

实现自己的迭代器

最近在写数据结构,使用类模板实现,碰到了一些问题,其中有一个就是遍历的问题,查阅资料最后实现了自己的迭代器,让我实现的数据结构能像STL一样进行for循环遍历。

类的构成

#include <stdexcept>
#include <iterator>
template <typename T>
class Queue
{
public:// 迭代器class iterator;class const_iterator;typedef std::reverse_iterator<iterator> reverse_iterator;typedef std::reverse_iterator<const_iterator> const_reverse_iterator;Queue() : _capacity(5), _size(0), _items(new T[_capacity]), _front(0), _rear(-1) {}~Queue();// 入队void enqueue(const T &item);// 出队T dequeue();// 查看队首元素T front() const;// 查看队尾元素T rear() const;// 获取队列元素数量unsigned int size() const;iterator begin() noexcept;iterator end() noexcept;const_iterator begin() const noexcept;const_iterator end() const noexcept;reverse_iterator rbegin() noexcept;reverse_iterator rend() noexcept;const_reverse_iterator rbegin() const noexcept;const_reverse_iterator rend() const noexcept;private:// 存储元素的数组T *_items;// 队列当前容量unsigned int _capacity;// 队列当前大小unsigned int _size;// 队首unsigned int _front;// 队尾unsigned int _rear;// 调整队列的大小void resize();// 判断队列是否为空bool empty() const;// 判断当前队列是否为满bool full() const;
};

可以看到,我声明了两个类iteratorconst_iterator,这两个类就是需要我们自己定义的迭代器。

官方定义

template <class Category, class T, class Distance = ptrdiff_t,class Pointer = T*, class Reference = T&>struct iterator {typedef T         value_type;typedef Distance  difference_type;typedef Pointer   pointer;typedef Reference reference;typedef Category  iterator_category;};

对于官方定义的迭代器结构,有5个参数,在后续实现自己的迭代器使需要设置。

自定义迭代器的实现

iterator

template <typename T>
class Queue<T>::iterator
{
public:// 标准写法// 迭代器的类型using iterator_category = std::bidirectional_iterator_tag;// 数据类型using value_type = T;// 迭代器之间的逻辑距离using difference_type = std::ptrdiff_t;// 指针using pointer = T*;// 引用using reference = T&;// 构造函数iterator(pointer data = nullptr,unsigned int pos = 0,unsigned int capacity = 0,unsigned int start = 0,unsigned int size = 0): _data(data), _pos(pos), _capacity(capacity), _start(start), _size(size) {}// 实现运算符重载// 解引用reference operator*() const {return this->_data[(this->_start + this->_pos) % this->_capacity];}// 成员访问pointer operator->() const{return &(operator*());}// 前++运算符iterator& operator++(){++this->_pos;return *this;}// 后++运算符iterator operator++(int){iterator tmp = *this;++this->_pos;return tmp;}// 前--运算符iterator& operator--(){--this->_pos;return *this;}// 后--运算符iterator operator--(int){iterator tmp = *this;--this->_pos;return tmp;}// 比较运算符bool operator==(const iterator& other) const{return this->_pos == other._pos;}bool operator!=(const iterator& other) const{return !(*this == other);}private:pointer _data;          // 指向队列数据数组的指针unsigned int _pos;      // 当前逻辑位置unsigned int _capacity; // 队列容量unsigned int _start;    // 队列实际起始位置unsigned int _size;     // 队列当前元素数量
};

const_iterator

template <typename T>
class Queue<T>::const_iterator
{// 标准写法// 迭代器的类型using iterator_category = std::bidirectional_iterator_tag;// 数据类型using value_type = const T;// 迭代器之间的逻辑距离using difference_type = std::ptrdiff_t;// 指针using pointer = const T*;// 引用using reference = const T&;// 构造函数const_iterator(const pointer data = nullptr,unsigned int pos = 0,unsigned int capacity = 0,unsigned int start = 0,unsigned int size = 0): _data(data), _pos(pos), _capacity(capacity), _start(start), _size(size) {}// 实现运算符重载// 解引用reference operator*() const {return this->_data[(this->_start + this->_pos) % this->_capacity];}// 成员访问pointer operator->() const{return &(operator*());}// 前++运算符const_iterator& operator++(){++this->_pos;return *this;}// 后++运算符const_iterator operator++(int){const_iterator tmp = *this;++this->_pos;return tmp;}// 前--运算符const_iterator& operator--(){--this->_pos;return *this;}// 后--运算符const_iterator operator--(int){const_iterator tmp = *this;--this->_pos;return tmp;}// 比较运算符bool operator==(const const_iterator& other) const{return this->_pos == other._pos;}bool operator!=(const const_iterator& other) const{return !(*this == other);}private:pointer _data;          // 指向队列数据数组的指针unsigned int _pos;      // 当前逻辑位置unsigned int _capacity; // 队列容量unsigned int _start;    // 队列实际起始位置unsigned int _size;     // 队列当前元素数量
};

begin()和end()的实现

template <typename T>
typename Queue<T>::iterator Queue<T>::begin() noexcept
{return iterator(this->_items, 0, this->_capacity, this->_front, this->_size);
}template <typename T>
typename Queue<T>::iterator Queue<T>::end() noexcept
{return iterator(this->_items, this->_size, this->_capacity, this->_front, this->_size);
}template <typename T>
typename Queue<T>::const_iterator Queue<T>::begin() const noexcept
{return const_iterator(this->_items, 0, this->_capacity, this->_front, this->_size);
}template <typename T>
typename Queue<T>::const_iterator Queue<T>::end() const noexcept
{return const_iterator(this->_items, this->_size, this->_capacity, this->_front, this->_size);
}

rbegin()和rend()的实现

template <typename T>
typename Queue<T>::reverse_iterator Queue<T>::rbegin() noexcept
{return reverse_iterator(this->end());
}template <typename T>
typename Queue<T>::reverse_iterator Queue<T>::rend() noexcept
{return reverse_iterator(this->begin());
}template <typename T>
typename Queue<T>::const_reverse_iterator Queue<T>::rbegin() const noexcept
{return const_reverse_iterator(this->end());
}template <typename T>
typename Queue<T>::const_reverse_iterator Queue<T>::rend() const noexcept
{return const_reverse_iterator(this->begin());
}

其他代码自己就不放出来了,主要是为了说明自定义迭代器,可以参照我的代码实现自己的迭代器。

测试

void testQueue()
{Queue<int> que;que.enqueue(4);que.enqueue(2);que.enqueue(5);std::cout << que.rear() << std::endl;for(const auto& item : que){std::cout << item << " ";}std::cout << std::endl;for(auto it = que.rbegin(); it != que.rend(); ++it){std::cout << *it << " ";}std::cout << std::endl;
}int main() {testQueue();return 0;
}

输出的结果为:

5
4 2 5
5 2 4
http://www.dtcms.com/wzjs/835405.html

相关文章:

  • 上海史特做网站多少钱青岛栈桥导游词
  • html网站源码下载国内知名网站建设公司
  • 高端网站设计哪家公司好广州企业网站找哪里
  • 企业网站形象建设wordpress shop路径在哪儿
  • 江西省美丽乡村建设公布网站asp企业网站开发技术
  • 网站内容排版设计杭州知名网页设计服务商
  • 网上做外贸都有哪些网站租用阿里云做网站
  • 自己怎么做视频收费网站合肥seo搜索优化
  • 张掖网站设计公司网络工程师app
  • 社交网站盈利吗传奇游戏网站怎么做
  • 伍佰亿网站怎么样长沙网站优化
  • 如何建淘客网站营销策划方案步骤
  • html网站搭建seo高级优化技巧
  • 公司网站制作范文怎么设置 多个首页 wordpress
  • 外贸做网站重庆关键词优化平台
  • 广南酒店网站建设樊城网站建设
  • 韩城搜索引擎建设网站3d设计房子的软件
  • 天津网站建设推广服务写作网站免费
  • 寿光建设局网站中企动力科技股份有限公司销售
  • 北京php网站建设信息技术课做网站
  • 学校门户网站做肥料网站
  • 简要概括自建网站的优缺点用插件做的炫酷网站
  • 郑州网站制作十年乐云seo梦幻西游官网
  • 国外网站空间租用哪个好母婴网站建设 社区
  • 北京网站建设推做网站要买什么空间
  • 室内设计有哪些网站自己建设网站需要什么
  • 有没有做网站的联系方式网站建设对于企业发展的优势
  • 高端建站需要什么条件企业网站的布局类型
  • 网站备案是什么推文关键词生成器
  • 广西网站制作公司青年人爱看的网站