MyArr.h
#pragma once #includeusing namespace std; class MyArray { public: MyArray(); //默认构造函数 explicit MyArray(int capacity); //有参构造函数,可自己指定容量初始化 MyArray(const MyArray& array); //拷贝构造 void Insert_Date(int pos, int val); //根据位置添加元素 int Get_Data(int pos); //获得指定位置的数据 int Get_Index(int val); //获取元素的索引,从左到右 void Push_Back(int val); //尾插法 void Pop_Back(); //尾删法 int Get_Size(); //获得长度 int Get_Capacity(); //获取容量 int Get_Max(); //获取最大值 int Get_Min(); //获取最小值 void Reverse(); //反转 int Get_Back(); //获取最后一个元素 ~MyArray(); //析构函数,释放数组空间 private: int mCapacity; //容量 int nSize; //当前数组中元素的个数 int* pAddress; //数组首地址 };
MyArr.cpp
#include "MyArr.h"
//默认构造函数
MyArray::MyArray()
{
cout << "默认构造函数的调用" << endl;
this->mCapacity = 0; //数据的初始化
this->nSize = 0;
pAddress = new int[mCapacity];
}
//有参构造函数,可自己指定 容量、初始化
MyArray::MyArray(int capacity)
{
cout << "有参构造函数的调用" << endl;
this->mCapacity = capacity;
pAddress = new int[mCapacity];
this->nSize = 0;
//pAddress[capacity] = { 0 };
}
//拷贝构造
MyArray::MyArray(const MyArray& array)
{
cout << "拷贝构造函数的调用" << endl;
pAddress = new int[array.mCapacity];
this->mCapacity = array.mCapacity;
for (int i = 0; i < array.mCapacity; ++i){
this->pAddress[i] = array.pAddress[i];
}
}
//添加元素
void MyArray::Insert_Date(int pos, int val){
if (pos<0 || pos>=mCapacity){
return;
}
for (int i = nSize - 1; i >= pos; --i)
{
this->pAddress[i + 1] = pAddress[i]; //地址右移
}
this->pAddress[pos] = val;
this->nSize++;
}
//获得当前索引的值
int MyArray::Get_Data(int index){
if (index<0 || index>mCapacity - 1){
return -1;
}
return this->pAddress[index];
}
//获取指定元素的索引值 从左到右
int MyArray::Get_Index(int val) {
for (int i = 0; i < this->nSize; i++) {
if (val == pAddress[i]) {
return i;
}
}
return -1;
}
//尾插法
void MyArray::Push_Back(int val){
if (nSize == mCapacity){
return;
}
this->pAddress[nSize] = val;
this->nSize++;
}
//尾删法
void MyArray::Pop_Back() {
if (nSize == 0) {
return;
}
nSize--;
}
//获得长度
int MyArray::Get_Size(){
return this->nSize;
}
//获取容量
int MyArray::Get_Capacity() {
return this->mCapacity;
}
//获取最大值
int MyArray::Get_Max() {
if (this->nSize == 0) {
return 0;
}
int temp = pAddress[0];
for (int i = 0; i < this->nSize; i++) {
if (temp < pAddress[i]) {
temp = pAddress[i];
}
}
return temp;
}
//获取最小值
int MyArray::Get_Min() {
if (this->nSize == 0) {
return 0;
}
int temp = pAddress[0];
for (int i = 0; i < this->nSize; i++) {
if (temp > pAddress[i]) {
temp = pAddress[i];
}
}
return temp;
}
void MyArray::Reverse() {
for (int i = 0; i < nSize; i++) {
//int temp;
//temp = pAddress[i];
//pAddress[i] = new int[pAddress[nSize - i - 1]];
//pAddress[nSize - i - 1] = temp;
}
}
int MyArray::Get_Back() {
int* fAddress = pAddress + nSize-1;
return *fAddress;
}
//析构函数,释放数组空间
MyArray::~MyArray(){
cout << "析构函数的调用" << endl;
if (pAddress != NULL)
{
delete[] pAddress;
}
pAddress = NULL;
}
main.cpp
#include"MyArr.h"
void test()
{
MyArray* p = new MyArray(30);
MyArray p2(*p);
for (int i = 0; i < 10; ++i){
p2.Push_Back(i);
}
for (int i = 0; i < p2.Get_Size(); ++i){
cout << p2.Get_Data(i) << " ";
}
cout << endl;
p2.Insert_Date(2, 9);
for (int i = 0; i < p2.Get_Size(); ++i){
cout << p2.Get_Data(i) << " ";
}
cout << endl;
int a = p2.Get_Data(2);
cout << "a = " << a << endl;
int b = p2.Get_Index(10);
cout << "b = " << b << endl;
int c = p2.Get_Min();
cout << "c = " << c << endl;
int d = p2.Get_Back();
cout << "d = " << d << endl;
}
int main()
{
test();
system("pause");
return 0;
}



