基于java的數據結構學習——動態數組C++類模板(含拷貝構造,重載常見運算符)

之前實現了java的動態數組,試著寫了個C++版的,同樣對時間復雜度振蕩進行了處理。純手打,代碼如下 :

//
// Created by PC-Saw on 2018/12/19.
//#ifndef DATA_STRUCTURE_MYARRAY_H
#define DATA_STRUCTURE_MYARRAY_H#include <iostream>using namespace std;template <typename T>
class MyArray {
private:int mSize;int mCapacity;T*  data;
public:MyArray();                                  // 無參構造函數explicit MyArray(int Capacity);             // 構造函數,傳入容量CapacityMyArray(const MyArray& arr);                // 拷貝構造MyArray &operator= (MyArray arr);           // 重載賦值操作符T &operator[](int index);                   // 重載[]操作賦~MyArray();                                 // 析構函數//友元函數實現 重載輸出 << 操作符friend ostream & operator << (ostream &out, MyArray<T> &obj){out << "MyArray size = " << obj.mSize << ", Capacity = " << obj.mCapacity << endl;out << "MyArray: [";for (int i = 0; i < obj.mSize; ++i){out << obj.data[i];if (i != obj.mSize - 1)out << ", ";}out << "] ";return out;}//友元函數實現 輸入 >> 操作符friend istream & operator >> (istream &in, MyArray<T> &obj){for (int i = 0; i < obj.mSize; ++i) {in << obj.data[i];}if (!in){obj = new MyArray();}return in;}bool isEmpty();                             // 判斷數組是否為空bool isFull();                              // 判斷數組是否已滿int  find(T t);                             // 查找元素 t 的下標bool contain(T t);                          // 判斷是否包含元素 tint  getSize();                             // 獲取數組元素個數int  getCapacity();                         // 獲取數組容量void pushFront(T t);                        // 頭部添加元素void pushBack(T t);                         // 尾部添加元素void insert(int index, T t);                // 在指定位置插入元素 tT    remove(int index);                     // 刪除指定位元素T    removeFront();                         // 刪除第一個元素T    removeBack();                          // 刪除最后一個元素void removeElement(T t);                    // 刪除元素 tvoid set(int index, T t);                   // 將指定位置元素設為 tT    get(int index);                        // 查看 index 位置的元素T    getBack();                             // 返回最后一個元素void resize(int newCapacity);               // 重新分配空間
};// 構造函數
template <typename T>
MyArray<T>::MyArray(int Capacity)
{cout << "調用 MyArray(int) 構造 " << endl;if (Capacity <= 0)throw "傳入容量有誤!";mSize     = 0;mCapacity = Capacity;data      = new T[Capacity];
};// 無參構造函數
template <typename T>
MyArray<T>::MyArray()
{cout << "調用 MyArray() 構造 " << endl;mSize     = 0;mCapacity = 10;data      = new T[10];
};// 拷貝構造
template <typename T>
MyArray<T>::MyArray(const MyArray<T>& arr)
{cout << "調用拷貝構造 " << endl;this->mSize     = arr.mSize;this->mCapacity = arr.mCapacity;this->data      = new T[arr.mCapacity];// 拷貝數據for (int i = 0; i < arr.mSize; ++i) {this->data[i] = arr.data[i];}
}// 重載賦值操作符
template <typename T>
MyArray<T> &MyArray<T>::operator= (const MyArray<T> arr)
{cout << "調用 = 賦值操作 " << endl;if (this->data != NULL){delete[] this->data;this->data = NULL;}//分配內存this->mSize     = arr.mSize;this->mCapacity = arr.mCapacity;this->data      = new T[arr.mCapacity];//拷貝數據for(int i = 0; i < this->mSize; i++){//如果是自定義的復雜數據類型,必須對 = 運算賦進行重載,  operator=this->data[i] = arr.data[i];}return *this;
}// 重載[]操作賦
template <typename T>
T &MyArray<T>::operator[](int index)
{if (index < 0 || index > this->mSize - 1)throw "索引非法!";return this->data[index];
}// 析構函數
template <typename T>
MyArray<T>::~MyArray()
{cout << "調用析構函數 " << endl;if (this->data != NULL){delete[] this->data;this->data  = NULL;}this->mSize     = 0;this->mCapacity = 0;
}// 判斷數組是否為空
template <typename T>
bool MyArray<T>::isEmpty()
{return mSize == 0;
};// 判斷數組是否已滿
template <typename T>
bool MyArray<T>::isFull()
{return mSize == mCapacity;
};// 查找元素 t 的下標
template <typename T>
int  MyArray<T>::find(T t)
{for (int i = 0; i < mSize; ++i){if (data[i] == t)return i;}return -1;
}// 查找是否包含元素 t
template <typename T>
bool MyArray<T>::contain(T t)
{return (find(t) != -1);
}// 獲取數組元素個數
template <typename T>
int  MyArray<T>::getSize()
{return mSize;
}// 獲取數組容量
template <typename T>
int  MyArray<T>::getCapacity()
{return mCapacity;
}// 頭部添加元素
template <typename T>
void MyArray<T>::pushFront(T t)
{insert(0, t);
}// 尾部添加元素
template <typename T>
void MyArray<T>::pushBack(T t)
{insert(mSize, t);
}// 在指定位置插入元素 t
template <typename T>
void MyArray<T>::insert(int index, T t)
{if (index < 0 || index > mSize) // 判斷下標是否有誤throw 0;if (isFull())                   // 數組已滿則重新分配空間resize(2 * mCapacity);for (int i = mSize; i > index ; --i){data[i] = data[i - 1];}data[index] = t;mSize++;
}// 刪除指定位元素
template <typename T>
T MyArray<T>::remove(int index)
{if (index < 0 || index >= mSize)                    // 判斷下標是否合法throw 0;// 刪除 index 位置的元素并返回T ret = data[index];for (int i = index; i < mSize - 1; ++i) {data[i] = data[i + 1];}mSize--;if (mSize == mCapacity / 4 && mCapacity / 2 != 0)   // 空閑空間太大,重新分配空間resize(mCapacity / 2);return ret;
}// 刪除第一個元素
template <typename T>
T MyArray<T>::removeFront()
{return remove(0);
}// 刪除最后一個元素
template <typename T>
T MyArray<T>::removeBack()
{return remove(mSize - 1);
}// 刪除元素 e
template <typename T>
void MyArray<T>::removeElement(T t)
{int index = find(t);index != -1 && remove(index);
}// 將制定位置元素設為 t
template <typename T>
void MyArray<T>::set(int index, T t)
{if (index < 0 || index >= mSize)throw 0;data[index] = t;
}// 返回 index 位置的元素
template <typename T>
T MyArray<T>::get(int index)
{if (index < 0 || index >= mSize)throw "index is illegal!";return data[index];
}// 返回最后一個元素
template <typename T>
T MyArray<T>::getBack()
{return get(mSize - 1);
}// 重新分配空間
template <typename T>
void MyArray<T>::resize(int newCapacity)
{if (newCapacity <= 0)throw 0;T* tmp = new T[newCapacity];for (int i = 0; i < mSize; ++i){tmp[i] = data[i];}delete[] data;data = tmp;mCapacity = newCapacity;
}#endif //DATA_STRUCTURE_MYARRAY_H

測試代碼:

#include <iostream>
#include "MyArray.h"using namespace std;int main()
{//cout << boolalpha;        // 將bool值正常顯示MyArray<int> arr;           for(int i = 0; i < 10; ++i) arr.pushBack(i);cout << arr << endl;arr.pushFront(100);cout << arr << endl;arr.pushFront(-1);cout << arr << endl;arr.remove(2);cout << arr << endl;arr.removeElement(4);cout << arr << endl;arr.removeFront();cout << arr << endl;for(int i = 0; i < 5; ++i){arr.removeBack();cout << arr << endl;}return 0;
}

?

之后還會有其他數據結構的 java 和 C++ 實現。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/448033.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/448033.shtml
英文地址,請注明出處:http://en.pswp.cn/news/448033.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

科目三考試過程詳解

科目三是考駕照的最后一項考試&#xff0c;所以考生在這關都很注意&#xff0c;但是有可能是由于過于緊張都難免會有些失誤&#xff0c;如果沒過的話&#xff0c;那也就意味著您拿照的時間又延長了另外還要交補考費。因此很多學員都想一次性把這項考試通過&#xff0c;那么我們…

圖解 IDEA 中 springboot 項目 MyBatis Generator 逆向生成實體類及 mapper 配置文件

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 一、準備工作&#xff1a; 1. 新建一個 配置文件&#xff1a;generatorConfig.xml 。 <?xml version"1.0" encoding&qu…

關于IIS 7.5 限制連接數與流量限制模塊

網頁中的視頻是用戶喜聞樂見的常見形式之一&#xff0c;并在主要的站點中中以某種形式&#xff08;產品視頻、教程視頻、理財場景、user generated content、消費報告等&#xff09;在更廣泛的應用。 其中的一個挑戰是把視頻加入到站點&#xff0c;雖然這并不花費很多代價。高質…

2014版學車考駕照精華攻略 總有一個你需要!趕緊收藏吧!!

新交規&#xff0c;新駕考&#xff0c;拿下本本&#xff0c;著實不容易。2013的你&#xff0c;是否已經踏上學車征程&#xff0c;為了順利拿到本本而苦于八方搜索&#xff0c;四處奔波&#xff0c;一心只為獲得有所幫助的經驗之談、簡單易懂的學車攻略呢&#xff1f;本著鋤強扶…

mybatis 逆向工程生成的 Example 類的使用

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 一.逆向工程 逆向工程可以針對單表自動生成 mybatis 執行所需要的代碼&#xff08;mapper.java,mapper.xml、po&#xff09;, 根據數據…

牛客假日團隊賽8

牛客假日團隊賽8 A Cell Phone Network 思路&#xff1a;最小支配集AC代碼#include<stdio.h> #include<iostream> #include<math.h> #include<algorithm> #include<string.h> #include<queue> #include<set> #include<string>…

汽車標志大全 買車必知

簡要介紹&#xff1a;為您提供汽車標志、世界汽車標志大全、各種汽車標志、國產汽車標志大全、汽車標志圖片、汽車標志及名稱、名車標志大全、世界名車排行榜、世界十大名車、世界名車圖片等有關汽車標志、汽車圖片、汽車名字及汽車品牌方面的知識。 歐美汽車標志圖片大全_歐美…

解決: Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. 報錯&#xff1a; Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver 2.但是&…

Python與MySQL連接

import MySQLdb #注意大小寫&#xff01;&#xff01;#建立和數據庫系統的連接conn MySQLdb.connect(hostlocalhost,userroot,passwdsmile,dbtest)#獲取操作游標cursor conn.cursor()#執行SQL,創建一個數據庫.cursor.execute("""create database python"…

科目三靠邊停車技巧要領

正在準備科目三的您&#xff0c;對順利通過考試有信心嗎&#xff1f;今天&#xff0c;小編為大家帶來科目三靠邊停車技巧&#xff0c;通過講解靠邊停車考試要求&#xff0c;讓學員更好地掌握相關技巧&#xff0c;希望能幫到大家。 靠邊停車考試項目中規定&#xff0c;車前保險杠…

解決:Field xxMapper in xx.service.impl.xxServiceImpl required a bean of type ‘xx.mapper.xxMapper‘

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. 啟動 springboot 項目報錯&#xff1a; Field userMapper in gentle.service.impl.UserServiceImpl required a bean of type gent…

dojo 九 effects dojo/_base/fx 和 dojo/fx

官方教程&#xff1a;Dojo Effects這里講學習一下dojo如何實現淡入、淡出、滑動等效果。實現這些特殊的效果有兩個包 dojo/_base/fx 和 dojo/fx。dojo/_base/fx 中提供了一些基礎的animation方法&#xff0c;如&#xff1a; animateProperty, anim, fadeIn, and fadeOut.dojo/f…

電子路考容易犯錯的五大細節

正在學車的你&#xff0c;知道在電子路考中哪些是考生常犯的錯誤嗎&#xff1f;下面&#xff0c;小編為大家帶來學車考生參加科目三考試特別容易犯錯的地方&#xff0c;尤其是不按規定使用轉向燈和在超車時不能根據道路交通情況合理選擇行車道或速度這兩項犯錯的人最多。 ●起步…

Linux 查看 MySQL 版本的四種方法

1 在終端下執行 mysql -V 2 在help中查找 mysql --help |grep Distrib 3 在mysql 里查看 select version() 4 在mysql 里查看 status 轉自&#xff1a;https://blog.csdn.net/chengyuc/article/details/77094775

html 基本布局介紹

1、div默認是縱向排列的&#xff0c;例子如下&#xff1a; <div id"wrap"><div id"div1">div1</div><div id"div2">div2</div><div id"div3">div3</div> </div> 2、如果要div橫向排列…

考駕照重點科目的關鍵考試技巧

定點停車停不好關鍵在于方向盤打得太晚&#xff0c;而且剎車沒有控制好&#xff01;剎車和方向應該同步進行&#xff0c;方向盤不要打得太多。上坡停車或者3檔以下停車可以先踩離合器&#xff0c;4-5檔停車必須先剎車減速以后再踩離合器。 上坡定點停車步驟&#xff1a;聽到指令…

第一階段小結

python簡介 定義&#xff1a;是一個免費&#xff0c;開源&#xff0c;跨品臺&#xff0c;動態&#xff0c;面向對象的編程語言 程序執行方式&#xff1a; 交互式&#xff1a;在命令行輸入指令&#xff0c;回車即可得到結果文件式&#xff1a;將指令編寫到py文件中&#xff0c;可…

@JsonSerialize 使用:注解方式 實現條件判斷屬性值、條件修改屬性值

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. 數據庫中是 1、0 記錄性別。 預期效果為&#xff1a;當查到屬性值為 1 時&#xff0c;就給序列化后的 json 中性別字段賦值為 “男”…

科目二、科目三易掛項目整理和網友支招

雖然已順利的拿到小黑本本&#xff0c;成了殺手&#xff0c;但回想起學車的種種經歷&#xff0c;不免感慨萬千&#xff01;感謝各位網友提供的種種信息&#xff0c;我才能很有信心的順利通過考試。因此&#xff0c;想把一些失敗的經歷匯總起來為后來者提個醒&#xff0c;做到心…

我該如何介紹我自己

今天和銷售總監一起去見客戶&#xff0c;早上8&#xff1a;47分的高鐵到蘇州。6&#xff1a;30起床&#xff0c;7點趕地鐵……上了高鐵&#xff0c;和銷售總監閑聊幾句&#xff0c;看了一點雜志耳邊就聽到列車廣播傳出優美的妹子聲“蘇州園區到了……”。25分鐘就到了蘇州&…