日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++实现顺序表的相关操作

發布時間:2023/11/30 c/c++ 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++实现顺序表的相关操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Myarray.h文件

#pragma once#include<iostream>using namespace std;class MyArray { public:MyArray();//默認構造 默認100容量MyArray(int capacity);MyArray(const MyArray& array);~MyArray();//尾插法void Push_Back(int val);//根據索引獲取值int getData(int index);//根據索引設置值void setData(int index, int val);//獲取數組大小int getSize();//獲取數組容量int getCapacity();private:int *pAddress;//指向真正存儲數據的指針int m_Size;//數組的大小int m_Capacity;//數組容量 };

Myarray.cpp

#include"Myarray.h"//默認構造 MyArray::MyArray() {this->m_Capacity = 100;this->m_Size = 0;this->pAddress = new int[this->m_Capacity]; } //有參構造 參數 數組容量 MyArray::MyArray(int capacity) {cout << "有參構造調用" << endl;this->m_Capacity = capacity;this->m_Size = 0;this->pAddress = new int[this->m_Capacity]; }//拷貝構造 MyArray::MyArray(const MyArray & array) {cout << "拷貝構造的調用" << endl;this->pAddress = new int[array.m_Capacity];this->m_Size = array.m_Size;this->m_Capacity = array.m_Capacity;for (int i = 0; i < array.m_Size; i++){this->pAddress[i] = array.pAddress[i];}}//析構 MyArray::~MyArray() {if (this->pAddress != NULL){cout << "析構函數的調用" << endl;delete[]this->pAddress;this->pAddress = NULL;} }void MyArray::Push_Back(int val) {//判斷越界?用戶自己處理this->pAddress[this->m_Size] = val;this->m_Size++; }int MyArray::getData(int index) {return this->pAddress[index]; }void MyArray::setData(int index, int val) {this->pAddress[index] = val; }int MyArray::getCapacity() {return this->m_Capacity; } int MyArray::getSize() {return this->m_Size; }

test.cpp

#include"Myarray.h"void test01() {//堆區創建數組MyArray *array = new MyArray(30);MyArray *array2=new MyArray(*array);//new方式指定拷貝構造MyArray array3 = *array2; //構造函數返回的本體//MyArray *array4 = array; //這聲明一個指針和array執行的地址相同,所以不會調用拷貝構造delete array;//尾插法的測試for (int i = 0; i < 10; i++){array2->Push_Back(i);}//獲取數據的測試for (int i = 0; i < 10; i++){cout << array2->getData(i) << endl;}//設置值的測試array2->setData(0, 1000);cout << array2->getData(0) << endl;//獲取數組大小cout << "array2的數組大小" << array2->getSize() << endl;//獲取數組容量cout << "array2的數組容量" << array2->getCapacity() << endl;}int main() {test01();system("pause");return 0; }

總結

以上是生活随笔為你收集整理的c++实现顺序表的相关操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。