Untitled

mail@pastecode.io avatar
unknown
abc
2 years ago
2.8 kB
6
Indexable
#include <iostream>
using namespace std;
class arr{
    int n;
    int *m  = new int[n];
    public:
        arr(){};
        arr(int n){
            for (int i = 0; i < n; i++)
            {
                m[i]=0;
            }
        }
        arr(int n,int *m){
            this->n=n;
            this->m=m;
        }
        arr(arr &mang){
            this->n= mang.getsize();
            for (int i = 0; i < n; i++)
            {
                this->m[i]=mang.get(i);
            }
        }
        
        ~arr(){
            delete []m;
        }

        void nhap(){
            cin>>n;
            for (int i = 0; i < n; i++)
            {
                cin>>m[i];
            }
            
        }

        int getsize(){
            return n;
        }

        int get(int n){
            if(n<this->n) return m[n];
            else cout<<"Oversize! ";
            return 0;
        }

        void set(int a,int b){
            m[a]=b;
        }

        int search(int a){
            for (int i = 0; i < n; i++)
            {
                if(a==m[i]) return i;
            }
            return -1;
        }
        bool ascending(int a,int b){
            return a>b;
        }
        bool descending(int a,int b){
            return a<b;
        }
        void arrange(bool c){
            if(c){
                for (int i = 0; i < n-1; i++)
                {
                    for (int j = i+1; j < n; j++)
                    {
                        if(ascending(m[i],m[j])){
                            int t=m[i];
                            m[i]=m[j];
                            m[j]=t;
                        }
                    }
                }
            }
            else{
                for (int i = 0; i < n-1; i++)
                {
                    for (int j = i+1; j < n; j++)
                    {
                        if(descending(m[i],m[j])){
                            int t=m[i];
                            m[i]=m[j];
                            m[j]=t;
                        }
                    }
                }                
            }            
        }

        void xuat(){
            for (int i = 0; i < n; i++)
            {
                cout<<m[i];
            }
            cout<<"\n";
        }



};



int main(){
    int n=3;
    int *m = new int[3]{0,1,2};
    arr m1(3);
    arr m2(n,m);
    arr m3(m2);
    arr m4;
    // m4.nhap();
    // m4.xuat();
    cout<<"Size: "<<m3.getsize()<<endl;
    m3.set(2,5);
    cout<<"Lay pt: "<<m3.get(2)<<endl;
    cout<<"Tim: "<<m3.search(1)<<endl;
    m3.arrange(0);
    m3.xuat();


    

}