C++自定义迭代器模板,实现ArrayList基本操作,自定义迭代器遍历

运用到的知识:(不分先后)

模板,类,循环数组,线性表,指针,异常,迭代器,输入输出等C++基础

#include<iterator>
#include<iostream>
#include<vector>
#include<cstring>
#include<string>
using namespace std;class EmptyListException :public exception
{
public:EmptyListException(){}
};
class InvalidIteratorException :public exception
{
public:InvalidIteratorException(){}
};template<class T>
class ArrayList {public:class Iterator : public iterator <input_iterator_tag, T> {public:Iterator(T* p,T* elem,int cnt) {_ptr = p;selfElems = elem;count = cnt;}bool operator != (const Iterator &iter) {return _ptr!= iter._ptr;}bool operator == (const Iterator &iter) {return _ptr == iter._ptr;}Iterator& operator ++ () {_ptr++;if(_ptr >= selfElems+count) _ptr = selfElems;return *this;}Iterator& operator -- () {_ptr--;if(_ptr < 0) _ptr = selfElems+count-1;return *this;}Iterator operator ++ (int) {Iterator tmp= *this;_ptr++;if(_ptr >= selfElems+count) _ptr = selfElems;return tmp;}Iterator operator -- (int) {Iterator tmp= *this;_ptr--;if(_ptr < 0) _ptr = selfElems+count-1;return tmp;}T& operator * () {return *_ptr;}public:T* _ptr;T* selfElems;int count;};ArrayList() {_selfElems = new T[4];_count = 4;init();}ArrayList(int n) {_selfElems = new T[n];_count = n;init();}~ArrayList() {delete[] _selfElems;}void init() {_front=0;_back=-1;nowsize = 0;}T& operator[](int i) {return _selfElems[i];}Iterator begin() {return Iterator(_selfElems+_front,_selfElems,_count);}Iterator end() {return Iterator(_selfElems + (_back + 1)%_count,_selfElems,_count);}T& front() {return _selfElems[_front];}T& back() {return _selfElems[_back];}int size() const {return nowsize;}void insertFront(T& e) {if(nowsize == _count) {T* _selfElems2 = new T[_count*2];for(int i = 0; i<_count; i++) {_selfElems2[i] = _selfElems[(_front+i)%_count];}delete[] _selfElems;_selfElems = _selfElems2;_front=0;_back=_count-1;_count*=2;}if(_front == 0) _front=_count-1;else _front--;_selfElems[_front] = e;nowsize++;}void insertBack(T&e) {if(nowsize == _count) {T* _selfElems2 = new T[_count*2];for(int i = 0; i<_count; i++) {_selfElems2[i] = _selfElems[(_front+i)%_count];}delete[] _selfElems;_selfElems = _selfElems2;_front=0;_back=_count-1;_count*=2;}_back = (_back+1) % _count;_selfElems[_back] = e;nowsize++;}void insert(Iterator p, T& e) {if(p._ptr < _selfElems || p._ptr >= _selfElems+_count||(begin()._ptr < end()._ptr && (p._ptr < begin()._ptr || p._ptr >= end()._ptr)) || (begin()._ptr > end()._ptr && (p._ptr < begin()._ptr && p._ptr >= end()._ptr))) {throw InvalidIteratorException();}if(nowsize == _count) {T* _selfElems2 = new T[_count*2];for(int i = 0; i<_count; i++) {_selfElems2[i] = _selfElems[(_front+i)%_count];}delete[] _selfElems;_selfElems = _selfElems2;_front=0;_back=_count-1;_count*=2;}int pos = p._ptr - begin().ptr;int len = (_back-1)-(pos+_front);for(int i = len; i>=0; i++) {int tar = i + pos+_front;_selfElems[(tar+1)%_count] = _selfElems[tar%_count];}_selfElems[pos+_front] = e;nowsize++;}void removeFront() {if(nowsize == 0) {throw EmptyListException();}_front = (_front+1)%_count;nowsize--;}void removeBack() {if(nowsize == 0) {throw EmptyListException();}_back = (_back+_count-1)%_count;nowsize--;}void remove(Iterator p) {if(nowsize == 0) {throw EmptyListException();}if(p._ptr < _selfElems || p._ptr >= _selfElems+_count||(begin()._ptr < end()._ptr && (p._ptr < begin()._ptr || p._ptr >= end()._ptr)) || (begin()._ptr > end()._ptr && (p._ptr < begin()._ptr && p._ptr >= end()._ptr))) {throw InvalidIteratorException();}int pos = p._ptr - begin()._ptr;//pos+_front ~ (back-1)int len = (_back-1)-(pos+_front);for(int i = 0; i<len; i++) {int tar = i + pos+_front;_selfElems[tar%_count] = _selfElems[(tar+1)%_count];}nowsize--;}private:int _count;int _front,_back,nowsize;T* _selfElems;};void testArrayListUnderflow() {// the first try blocktry {ArrayList<int> arr;int x = 1;arr.insertBack(x);arr.removeFront();arr.removeFront();printf("did not catch exception\n");} catch(EmptyListException& e) {printf("caught EmptyStackException\n");}// the second try blocktry {ArrayList<int> arr;int x = 1;arr.insertBack(x);arr.removeBack() ;arr.removeBack() ;printf("did not catch exception\n");} catch(EmptyListException& e) {printf("caught EmptyStackException\n");}// the third try blocktry {ArrayList<int> arr;int x = 1;arr.insertBack(x);ArrayList<int>::Iterator it = arr.begin();arr.remove(it);it = arr.begin();arr.remove(it);printf("did not catch exception\n");} catch(EmptyListException& e) {printf("caught EmptyStackException\n");}
}
void testIntegerIterator() {ArrayList<int> arr;int a[6] = {1,2,3,4,5,6};for(int i = 0; i<6; i++) arr.insertBack(a[i]);for(ArrayList<int>::Iterator iter = arr.begin(); iter != arr.end(); iter++) {cout << *iter << " ";}cout << endl;arr.removeFront();arr.removeFront();arr.removeFront();int b[3] = {7,8,9};arr.insertBack(b[0]);arr.insertBack(b[1]);arr.insertBack(b[2]);for(ArrayList<int>::Iterator iter = arr.begin(); iter != arr.end(); iter++) {cout << *iter << " ";}cout << endl;
}
void testStringIterator() {ArrayList<string> arr;string a[6] = {"s1","s2","s3","s4","s5","s6"};for(int i = 0; i<6; i++) arr.insertBack(a[i]);for(ArrayList<string>::Iterator iter = arr.begin(); iter != arr.end(); iter++) {cout << *iter << " ";}cout << endl;arr.removeFront();arr.removeFront();arr.removeFront();string b[3] = {"s7","s8","s9"};arr.insertBack(b[0]);arr.insertBack(b[1]);arr.insertBack(b[2]);for(ArrayList<string>::Iterator iter = arr.begin(); iter != arr.end(); iter++) {cout << *iter << " ";}cout << endl;
}
int main() {testArrayListUnderflow();testIntegerIterator();testStringIterator();return 0;
}

参考资料:

http://c.biancheng.net/view/471.html

https://blog.csdn.net/holyweng/article/details/82821646

https://blog.csdn.net/qq_36745117/article/details/83863190

https://blog.csdn.net/XiaoHeiBlack/article/details/77014626

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439342.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SharePoint2013 2019性能及限制

适用于&#xff1a;2013 2019 SharePoint Online 本文档介绍 SharePoint Server 2013 的软件边界和限制。其中包括&#xff1a; 边界&#xff1a; 根据设计无法超过的静态限制 阈值&#xff1a; 为满足特定要求而能够超过的可配置限制 支持的限制&#xff1a; 已默认设置为…

Anaconda安装绘图模块altair

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple altair

MOSS/Sharepoint RBS概念以及运用

注&#xff1a;原文作者不知道是谁&#xff0c;先收藏了这篇&#xff08;若有侵权&#xff0c;请留言&#xff0c;删除&#xff09; EBS -> External Blob Storage 外部大二进制对象存储 RBS -> Remote Blob Storage 远程大二进制对象存储 这俩概念据我所知&#xff0c;…

【Python学习】 - 超详细的零基础Pandas学习(附Python数据分析与应用课本第四章实训答案)

&#xff08;博文体系参考&#xff1a;《Python数据分析与应用》课本&#xff09; 任务数据如下&#xff1a; 读入csv文件时&#xff0c;encoding必须是正确的&#xff0c;常用的编码格式有&#xff1a;UTF-8 , UTF-16 , GBK , GB2312 , GB18030等。 如果和文件的编码格式不符…

SQL Server 中“dbo”到底是什么

在&#xff33;&#xff31;&#xff2c; Server中看见很多表名都有前缀dbo&#xff0c;但是不写也可以。比如pubs的数据库中的表dbo.title &#xff11;&#xff0e; dbo是一个构架(schema)&#xff0c;在sql2005中,表的调用格式如下:"数据库名.构架名.表名",同一个…

win10,配置环境变量时系统环境变量和用户环境变量的优先级

关于%%中间夹着的东西&#xff0c;比如%AppData%的路径&#xff0c;是在注册表中可以修改的&#xff0c;用户也可以自定义这种%...%&#xff0c;相当于你定义了一个路径常量&#xff0c;定义方法也是去系统的环境变量中去添加即可。 比如一般常用%JAVAHOME%这种。 参考文章&a…

sql server登录名、服务器角色、数据库用户、数据库角色、架构区别联系**

1.一个数据库用户可以对应多个架构&#xff08;架构是表容器&#xff09;。架构里面包含的是数据库表。 2.一个数据库角色有可能涉及多个架构。数据库角色对应的是权限。 3.一个用户对应一个数据库角色。 4.登录名与数据库用户在服务器级别是一对多的&#xff1b;在数据库级…

python 字典、列表、字符串 之间的相互转换

1、列表与字符串转换 列表转字符串&#xff1a; 将列表中的内容拼接成一个字符串 将列表中的值转成字符串 字符串转列表&#xff1a; 用eval转换 将字符串每个字符转成列表中的值 将字符串按分割成列表 2、列表与字典转换 列表转字典&#xff1a; 将两个列表转成字典 将嵌…

【转】SharePoint开发中可能用到的各种Context(上下文)

第一部分 服务器端上下文 一、HttpContext 这个……我想就不用再介绍了&#xff0c;SharePoint运行在标准的ASP.NET框架下&#xff08;2003用的不是标准的ASP.NET 1.1&#xff0c;不过这年头还有人用2003么&#xff09;&#xff0c;所以这个对象的使用和ASP.NET没有任何区别。…

Python 字符串前面加u,r,b,f的含义

1、字符串前加 u 例&#xff1a;u"我是含有中文字符组成的字符串。" 作用&#xff1a; 后面字符串以 Unicode 格式 进行编码&#xff0c;一般用在中文字符串前面&#xff0c;防止因为源码储存格式问题&#xff0c;导致再次使用时出现乱码。 2、字符串前加 r 例&a…

【转】aspx,ascx和ashx使用小结

做asp.net开发的对.aspx,.ascx和.ashx都不会陌生。关于它们&#xff0c;网上有很多文章介绍。“纸上得来终觉浅&#xff0c;绝知此事要躬行”&#xff0c;下面自己总结一下做个笔记。1、.aspx Web窗体设计页面。Web窗体页由两部分组成&#xff1a;视觉元素&#xff08;html、服…

【转】执行Import-SPWeb报错Import-SPWeb : Requested value 'PublishingPages' was not found

Import-SPWeb : Requested value PublishingPages was not found. 【解决办法】 1.找到CPM文件 2.把cpm文件后缀名改为cab&#xff0c;如果有多个cmp需要把所有文件改为cab 3.使用cmd全部解压&#xff0c;然后找到Manifest.xml 4.找到“PublishingPages”&#xff0c;替换为…

【Gym - 101775J】Straight Master(差分,思维)

题干&#xff1a; A straight is a poker hand containing five cards of sequential rank, not necessarily to be the same suit. For example, a hand containing 7 club, 6 spade, 5 spade, 4 heart and 3 diamond forms a straight. In this problem, we extend the defi…

【转】Azure DevOps —— Azure Board 之 长篇故事、特性、用户情景(故事)的用法应用场景

前提 我以前在之前的文章里大概介绍了 Azure Board 的基本使用&#xff0c;可以回看《Azure Board 的基本使用》。如果你想使用 Azure Board 来安排工作的话&#xff0c;请提前了解《敏捷开发》的相关知识。 作者将使用 “Agile” 作为项目的模板&#xff0c;不明白的先阅读《…

【VIJOS - P1037】搭建双塔(dp)

题干&#xff1a; 描述 2001年9月11日&#xff0c;一场突发的灾难将纽约世界贸易中心大厦夷为平地&#xff0c;Mr. F曾亲眼目睹了这次灾难。为了纪念“9?11”事件&#xff0c;Mr. F决定自己用水晶来搭建一座双塔。 Mr. F有N块水晶&#xff0c;每块水晶有一个高度&#xff0…

【转】10分钟精通SharePoint - VS开发模板

Visual Studio是SharePoint开发的最锋利的利器&#xff08;没有之一哦&#xff09;&#xff0c;是必不可少的开发工具。VS一直为SharePoint开发保驾护航。 提供了以下功能开发模板&#xff1a; WebPart &#xff1a;服务器端控件 SharePoint中最常用的展示控件之一&#xff…

【Tyvj - 1305】最大子序和(单调队列优化dp)

题干&#xff1a; 输入一个长度为n的整数序列&#xff0c;从中找出一段不超过M的连续子序列&#xff0c;使得整个序列的和最大。 例如 1&#xff0c;-3,5,1&#xff0c;-2,3 当m4时&#xff0c;S51-237 当m2或m3时&#xff0c;S516 输入格式 第一行两个数n,m 第二行有n个数&…

【转】SharePoint 2010 用户自定义编辑Meta标签的OOB方法

今天遇到一个小问题&#xff0c;客户想要动态定制页面的<Head>标签里Meta, 而且需要使用OOB. 考虑了一下&#xff0c;Meta 标签肯定是用来让Server Crawl的, 那JS的方法首先排除&#xff0c;那就只能用服务器控件了, 在做了一些测试后&#xff0c;Field Control 可以帮我…

【计蒜客 - 程序设计竞赛】商业信息共享(Tarjan缩点)

题干&#xff1a; 商业信息共享 有 N 个公司&#xff0c;从每个公司都能单向地向另外一个公司分享最新商业信息&#xff0c;因为他们之间有着某种合作&#xff0c;你需要解决两个问题&#xff1a; 现在有一个最新的商业信息&#xff0c;至少需要告诉多少个公司&#xff0c;使…

【转】SharePoint 编程指南

网站相关的对象模型 作为一个应用原型系统&#xff0c;SharePoint提供了很多强大的应用及管理功能&#xff0c;但是&#xff0c;在实际的应用中&#xff0c;这些原有的功能很难满足用户的需求。因此&#xff0c;SharePoint也提供了一套非常完整的对象模型作为底层接口&#xf…