• 神奇的array2


    #include <iostream>
    #include <cstring>
    using namespace std;
    
    class Array2 {
        int **p;
    public:
    
        Array2(){};
        Array2(int x,int y){
            p=new int*[x];
            for(int i=0;i<x;i++)
                p[i]=new int[y];
        }
        int *operator[](int index){return p[index];};
        int operator ()(int i,int j){ return p[i][j];};
    
    };
    
    int main() {
        Array2 a(3,4);
        int i,j;
        for(  i = 0;i < 3; ++i )
            for(  j = 0; j < 4; j ++ )
                a[i][j] = i * 4 + j;
        for(  i = 0;i < 3; ++i ) {
            for(  j = 0; j < 4; j ++ ) {
                cout << a(i,j) << ",";
            }
            cout << endl;
        }
        cout << "next" << endl;
        Array2 b;     b = a;
        for(  i = 0;i < 3; ++i ) {
            for(  j = 0; j < 4; j ++ ) {
                cout << b[i][j] << ",";
            }
            cout << endl;
        }
        return 0;
    }
    View Code

    好吧,还是要老老实实建一个二维数组(之前写法太蛇皮了2333)

    public:
        int * p=NULL;
        int *pp;
        int r,c;
        int mark=0;
        Array2 (int i,int j){
            int* tmp;
            tmp=new int[i*j];
            p=tmp;
            r=i;
            c=j;
        }
        Array2(){
            p=NULL;
        }
    
    
        Array2& operator[](int i){
            if(mark==0){
                pp=p+i*c;
                mark=1;}
            else{
                pp+=i;
                mark=0;
            }
            return *this;
        }
        friend ostream& operator<<(ostream& os,Array2 s){
            os<<*(s.pp);
            return os;
        }
        void operator=(int n){
            *(pp)=n;
        }
        void operator=(Array2& s){
            p=s.p;
            r=s.r;
            c=s.c;
        }
    int& operator()(int i,int j){
            return *(p+i*c+j);
        }
    
  • 相关阅读:
    面向过程思想理解:
    Ajax的get请求向服务器请求数据五步骤?
    什么是Ajax无刷新技术?
    Ajax和JavaScript的区别
    为什么要用ajax
    好文章分享
    easyui系列之表单二Combogrid,Form,filebox,CheckBox(3)
    属性vs字段的关系
    jQuery-Load方法
    .Net 百度经纬度转高德
  • 原文地址:https://www.cnblogs.com/maskoff/p/8641637.html
Copyright © 2020-2023  润新知