c++ 自己实现一个迭代器

具体代码

/*
    自定义迭代器的实现
*/
#include <iostream>
using namespace std;
class num
{
    int val;    //具体的数字
    int length; //数字的位数
    void calculate_length(){
        if(val/10==0){      //这个数字只有1位
            length=1;
            return;
        }
        int x=10;           //这里就是不断重复除直到为0,从而得出数字的具体位数
        int pow=0;
        int tempNum=val;
        while(tempNum!=0){
            tempNum/=10;
            pow++;
        }
        length=pow;
    }
public:
    num(int tempNum)
    { //以下是一些基本的函数,用于设置值
        val=tempNum;
        calculate_length();
    }
    void set(int tempNum)
    {
        val = tempNum;
        calculate_length();
    }
    int get()
    {
        return val;
    }
    //以下是迭代器的部分
    class iterator
    {
        int pos;    //数字的下标
        num* obj;   //如果要在迭代器里面访问num的内容,必须要这个
    public:
        /*
            迭代器,要重载*,++,--
        */
        iterator(num* ptr,int n)
        {
            pos = n;
            obj = ptr;
        }
        iterator()
        {
            //空构造器
            pos = 0;
            obj = nullptr;
        }
        //操作符
        void operator++(){  //注意,这种没有参数的++重载的是前置的++   ++it
            pos++;
        }
        void operator++(int i){  //这种有任意int参数的重载的是后缀++  it++
            pos++;
        }
        void operator--(){
            pos--;
        }
        void operator--(int i){  
            pos--;
        }
        int operator*()const{
            //13324 取第二位10位:  (13324%100)/10
            //num  去除第n位     (num % 10^(n))/ 10^(n-1)
            if(pos>=obj->length) return -1;
            if(pos==0)return obj->val%10;
 
            int o=10;
            int pow=0;
            while(pow<(pos-1)){
               // cout<<pow<<" "<<pos<<endl;
                o*=10;
                pow++;
            }
            return (obj->val%(o*10))/(o);
        }
        bool operator!=(const iterator& it){
            return it.pos!=pos;
        }
        bool operator==(const iterator& it){
            return it.pos==pos;
        }
    };
    //获取迭代器,常见的比如begin,end;
    iterator begin()
    {
        return iterator(this,0);
    }
    iterator end()
    {
        return iterator(this,length);
    }
};
int main()
{
    num a(2354862);
    for(auto it=a.begin();it!=a.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
    
    return 0;
}

参考资料

https://blog.csdn.net/dyyzlzc/article/details/103336232

相关推荐

  1. c++ 自己实现一个

    2024-03-19 21:52:04       44 阅读
  2. 模式-C++实现

    2024-03-19 21:52:04       53 阅读
  3. 模式-C#实现

    2024-03-19 21:52:04       56 阅读
  4. C# 的

    2024-03-19 21:52:04       72 阅读
  5. c++之与反向

    2024-03-19 21:52:04       43 阅读
  6. C++初阶-反向的模拟实现

    2024-03-19 21:52:04       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-19 21:52:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-19 21:52:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-19 21:52:04       87 阅读
  4. Python语言-面向对象

    2024-03-19 21:52:04       96 阅读

热门阅读

  1. 【Rust】——panic!和不可恢复的错误

    2024-03-19 21:52:04       46 阅读
  2. 深入理解nginx的请求限速模块[下]

    2024-03-19 21:52:04       39 阅读
  3. Winform编程详解十八:ContextMenuStrip 右键菜单

    2024-03-19 21:52:04       41 阅读
  4. 新手leetcode 126周赛被拷打篇

    2024-03-19 21:52:04       43 阅读
  5. 安卓UI面试题 36-40

    2024-03-19 21:52:04       35 阅读
  6. 网络编程day4

    2024-03-19 21:52:04       45 阅读
  7. 关于前端的学习2

    2024-03-19 21:52:04       38 阅读
  8. 人生视角的双重镜像:喜剧与悲剧的辩证统一

    2024-03-19 21:52:04       41 阅读