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

歡迎訪問 生活随笔!

生活随笔

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

c/c++

C++(19)--自定义Array,vector练习

發布時間:2023/12/13 c/c++ 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++(19)--自定义Array,vector练习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

自定義Array,vector

  • 1.自定義Array
  • 2.自定義vector

《老九學堂C++課程》《C++ primer》學習筆記。《老九學堂C++課程》詳情請到B站搜索《老九零基礎學編程C++入門》
-------------簡單的事情重復做,重復的事情用心做,用心的事情堅持做(老九君)---------------

1.自定義Array

//main.cpp #include <iostream> #include "Array.h" using namespace std; void TestArray(){Array arr1(10);cout << arr1;arr1[0] = 1234;cout << arr1; } int main() {//TestIntefer();//TestString();TestArray();return 0; } //Array.h // // Created by 陳瑩瑩 on 2021/3/5. // #ifndef CHAPTER12_ARRAY_H #define CHAPTER12_ARRAY_H #include <iostream> using namespace std;class Array { public:Array(int lenght = 0);// 拷貝構造函數Array(const Array & arr);// 賦值運算符重載const Array & operator=(const Array & arr);// 重載輸出運算符friend ostream & operator<<(ostream & out, const Array & arr);// 重載[],實現下標索引和下標賦值// int operator[](int index); // 獲取元素,無法寫入,這個編譯器只有返回值不同是不能夠重載的int & operator[](int index); // 可以修改了~Array(); private:int m_lenght;int *m_data; // 需要加啥?};#endif //CHAPTER12_ARRAY_H //Array.cpp // // Created by 陳瑩瑩 on 2021/3/5. // #include <iostream> #include <cstring> #include "Array.h" using namespace std;Array::Array(int lenght):m_lenght(lenght) {if(m_lenght == 0){m_data = NULL;}else{m_data = new int[m_lenght];} } Array::Array(const Array & arr){if(arr.m_lenght == 0){return;}m_lenght = arr.m_lenght;m_data = new int[m_lenght];memcpy(m_data, arr.m_data, m_lenght*sizeof(int));} const Array & Array::operator=(const Array & arr){if(this == &arr){return *this;}m_lenght = arr.m_lenght;m_data = new int[m_lenght];memcpy(m_data, arr.m_data, m_lenght*sizeof(int));return *this;} ostream & operator<<(ostream & out, const Array & arr){for(int i = 0; i < arr.m_lenght; i++){out << arr.m_data[i] << ";";}out << endl;return out; } //int Array::operator[](int index) { // if(m_lenght==0){ // cerr << "數組為空,訪問失敗!" << endl; // } // if(index < 0 || index >= m_lenght){ // cerr << "數組下標越界!" << endl; // } // return m_data[index]; //} int & Array::operator[](int index) {if(m_lenght==0){cerr << "數組為空,訪問失敗!" << endl;}if(index < 0 || index >= m_lenght){cerr << "數組下標越界!" << endl;}return m_data[index]; } Array::~Array() {delete[] m_data; }

2.自定義vector

模版技術來實現

//main.cpp #include <iostream> #include "MyVector.h" using namespace std;void TestVector(){MyVector<int> vec1; // 默認構造MyVector<double> vec2(10,99.9);cout << "vec1" << vec1 << endl;cout << "vec2" << vec2 << endl;MyVector<string> vec3(5, string("abc"));cout << "vec3" << vec3 << endl; // vec3.push_back("123"); // cout << "vec3" << vec3 << endl; // 沒成功 } int main() {//TestIntefer();//TestString();//TestArray();TestVector();return 0; } //Vector.h // // Created by 陳瑩瑩 on 2021/3/8. // 自定義的容器類 // 使用的模版技術,一般用來做算法,比如重載100次某個類型的算法 // 如果使用模版技術,那么類的聲明和方法實現都要放在同一個頭文件中int GetMax(int num1, int num2); double GetMax(double num1, double num2); // 寫起來太麻煩了,需要使用模版技術來簡化一下 //tempalte<typename T1> 提示要建立心的T模版類 //T1 GetMax1(T1 num1, T1 num2);#ifndef CHAPTER12_MYVECTOR_H #define CHAPTER12_MYVECTOR_H #include <iostream> #include <cstring> using namespace std;//template<class T> // 聲明了一個模版,之后這個T可以指代所有的東西(類型) template<typename T> // 新版本的C++的寫法class MyVector { public:MyVector();MyVector(int len, T element); // 填充len長度的元素elementMyVector(const MyVector<T> & vec); // Myvector<T>是這個類的類型,復制構造函數MyVector<T> & operator=(const MyVector<T> & vec);T & operator[](int index);void push_back(T element); // 將元素element 添加到內部數組中T pop_back(); // 返回并刪除最后一個元素void insert(int pos, T element); // 在pos位置處,插入元素elementvoid clear(); //清空所有的元素template<class T2>friend ostream & operator<<(ostream & out, const MyVector<T2> & vec);~MyVector(); private:T * m_elements; // 用來存放元素的數組int m_length; // 所存放的實際個數int m_capacity; // 當前元素數組的大小 };template<typename T> MyVector<T>::MyVector():m_capacity(16), m_length(0) {this->m_elements = new T[m_capacity]; } template<typename T> MyVector<T>::MyVector(int len, T element):m_capacity(16) {m_capacity = len + m_capacity;m_length = len;m_elements= new T[m_capacity];// 使用for 循環復制for(int i = 0 ; i < m_capacity; i++){//m_element[i] = elemnet; // 注意:這里每次都會調用重載的賦值運算符memcpy(&m_elements[i], &element, sizeof(T)); // 不用重載復制運算符} } template<typename T> MyVector<T>::MyVector(const MyVector<T> &vec) {m_capacity = vec.m_capacity;m_length = vec.m_length;m_elements = new T[m_capacity];memcpy(m_elements, vec.m_elements, m_length * sizeof(T)); } template<typename T> MyVector<T> & MyVector<T>::operator=(const MyVector<T> & vec) {if(this == vec) return *this;if(NULL != m_elements){ // 刪除原來的指針,產生新的指針delete[] m_elements;m_elements = NULL;}m_capacity = vec.m_length + vec.m_capacity;m_length = vec.m_length;m_elements= new T[vec.m_capacity];memcpy(m_elements, vec.m_elements, m_length * sizeof(T));return *this; } template<typename T> T & MyVector<T>::operator[](int index) {return m_elements[index]; }template<typename T> void MyVector<T>::push_back(T element) {if(NULL == m_elements){m_capacity = 16;m_length = 0;m_elements = new T[m_capacity];}// 判斷當前的數組容量是否已滿if(m_length == m_capacity){// 如果滿了,就擴容:當前容量*2+1T * newElements = new T[m_capacity * 2 + 1];// 把原來的元素拷貝到新空間中memcpy(newElements, m_elements, m_length * sizeof(T));delete[] m_elements;m_elements = newElements; // 指向新空間}// m_elements[m_length] = element; 要重載=memcpy(m_elements[m_length++], &element, sizeof(T)); } template<class T2> ostream & operator<<(ostream & out, const MyVector<T2> & vec) {for(int i = 0; i < vec.m_length; i++){out << vec.m_elements[i] << "; ";}out << endl;return out; }template<typename T> MyVector<T>::~MyVector() {delete[] m_elements; } #endif //CHAPTER12_MYVECTOR_H

總結

以上是生活随笔為你收集整理的C++(19)--自定义Array,vector练习的全部內容,希望文章能夠幫你解決所遇到的問題。

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