The realization of C++ dynamic array, and the composition of the class and calling the method | the encapsulation of the array class

Define a dynamic array class whose elements are integers, and call each function in the main function:

Data members: pointer to the first address of the element (used to store the dynamically requested array space), the capacity of the current array, and the number of current elements;

Member function:

1. Constructor with parameters/without parameters (dynamically apply for 10 element spaces without parameters and change the capacity of the current array and the value of the current element; process with parameters according to parameters);

2. Add an element at the end;

3. Modify the element value at the specified position;

4. Query whether the element of the specified data exists;

5. Output all elements in the array;

6. Increase the array capacity (dynamically apply for a new capacity array space, copy the elements in the original array space to the new space and modify the capacity value of the current array, release the delete original array space);

7. Destructor (release the dynamically allocated array space);

8. Binary query the elements of the specified data and return the original position (selected as, first define a structure array in the function to temporarily store the current element value and position. Second, use the sort function to sort the structure array, and finally, Use the binary (half) search method to find whether the specified element exists. If it exists, return the original position value. Otherwise, return -1. Release the space of the structure array before returning).

#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;

struct Array1
{
    int val;
    int index;//将动态数组用结构体保存,val为原始值,index为原始位置
};

class Array
{
    int *p;//地址指针
    int Size;//在动态数组的个数
    int Capacity;//动态数组容量
public:
    Array(int Capacity);//有参构造
    Array();//无参构造
    void add_last(int x);//在尾部增加一个数x
    void change_data(int index,int x);//修改index处的值为x
    void PrintAll();//打印所有元素
    int query(int x);//查询x是否存在于动态数组中,二分查找
    void AddCapacity(Array,int Capacity);//将数组复制到新的空间,并增加数组容量
    ~Array();//析构函数
};

Array::Array(int x)
{
    this->Capacity=x;
    this->Size=0;
    this->p=new int[this->Capacity];//创建堆区数组
}

Array::Array()
{
    Capacity=10;
    this->Size=0;
    this->p=new int[this->Capacity];
}

void Array::add_last(int x)
{
    if(this->Size==this->Capacity)
    {
        return;
    }
    this->p[this->Size]=x;
    this->Size++;
}

void Array::change_data(int index,int x)
{
    this->p[index-1]=x;
}

 bool Map(struct Array1 a,struct Array1 b){
     if(a.val<b.val) return true;
     else  return false;
 }//sort排序条件


int Array::query(int x)
{

    Array1 arr[1000];
    for(int i=0;i<this->Size;i++)
    {
        arr[i].index=i;arr[i].val=this->p[i];
    }
    sort(arr,arr+this->Size,Map);//按照数组值排序
    //二分查找
    int left=0;int right=this->Size-1;
    while(left<=right)
    {
        int middle=(left+right)/2;
        if(x==arr[middle].val) return arr[middle].index+1;
        if(x>arr[middle].val) left=middle+1;
        else right=middle-1;
    }return -1;
}

void Array::PrintAll()
{
    for(int i=0;i<this->Size;i++)
    {
        cout<<this->p[i]<<" ";
    }cout<<endl;
}

void Array::AddCapacity(Array arr,int x)
{
    this->Capacity=arr.Capacity+x;
    this->Size=arr.Size;
    //this->p=arr->p;  这是浅拷贝,直接把指针与p指向同一个,易发生内存泄漏
    this->p=new int[this->Capacity];//深拷贝,创建新堆区
    for(int i=0;i<arr.Size;i++)
    {
        this->p[i]=arr.p[i];
    }
}

Array::~Array()
{
    delete []this->p;
    this->p=NULL;
    this->Capacity=0;
    this->Size=0;
}

int main()
{
    int n,x;
    cout<<"请输入数组容量:";
    cin>>n;
    Array a(n);
    cout<<"请输入"<<n<<"个元素的值:"<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        a.add_last(x);
    }
    cout<<"打印数组:"<<endl;
    a.PrintAll();
    cout<<"将数组的容积增加了10"<<endl;
    a.AddCapacity(a,10);
    cout<<"修改2处的值为77:"<<endl;
    a.change_data(2,77);
    a.PrintAll();
    cout<<"查询是否有值等于9并返回所处位置:"<<endl;
    cout<<a.query(9)<<endl;
    a.~Array();
}

Can you give me a free like! !  

Guess you like

Origin blog.csdn.net/weixin_59798969/article/details/123753050