您好,登錄后才能下訂單哦!
本篇內容介紹了“C++怎么實現動態數組”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
動態數組Vector可以動態擴展內存,其采用連續的內存空間,當內存空間不足,便以原來的容量的2倍或者1.5倍成倍的擴展,將原有的數組元素拷貝到新分配的內存空間中,釋放原有的內存空間,新的元素將存入的新分配的內存空間。
動態數組vector的size函數和capacity函數,分別作為求數組中現有的元素的個數和數組所能容納的元素的個數。下面直接上實現的代碼。
DynamicArray .h
#pragma once class DynamicArray { public: DynamicArray(); ~DynamicArray(); void push_back_Array(int value); void insertValueByPosArray(size_t pos,int value); void removeByValueFromArray(int value); void removeByPosFromArray(size_t pos); int findPosByValueArray(int value); int findValueByPosArray(size_t pos); void reclaimSpaceArray(); void clearArray(); int getCapacity(); int getCount(); void printArray(); private: int *m_pArr; size_t m_size; size_t m_capacity; };
DynamicArray .cpp
#include "DynamicArray.h" #include <iostream> using namespace std; // DynamicArray.cpp DynamicArray::DynamicArray() { m_size = 0; m_capacity = 20; m_pArr = new int[m_capacity]; if (m_pArr == nullptr) { cout << "new 開辟空間失敗" << endl; } } DynamicArray::~DynamicArray() { if (m_pArr != nullptr) { delete[] m_pArr; m_pArr = nullptr; } m_size = 0; m_capacity = 0; } void DynamicArray::push_back_Array(int value)//push_back { if (m_pArr == nullptr) { return; } reclaimSpaceArray(); m_pArr[m_size] = value; m_size++; } void DynamicArray::insertValueByPosArray(size_t pos, int value)//插入insert(可以在前,中,后插入) { if (m_pArr == nullptr) { return; } reclaimSpaceArray(); for (size_t i = m_size - 1; i >= pos; --i)//pos為下標的數,從0開始 { m_pArr[i + 1] = m_pArr[i]; } m_pArr[pos] = value; m_size++; } void DynamicArray::removeByValueFromArray(int value) { if (m_pArr == nullptr) { return; } int nPos = findPosByValueArray(value); removeByPosFromArray(nPos); } void DynamicArray::removeByPosFromArray(size_t pos)//pos為下標的數,從0開始 { if (m_pArr == nullptr) { return ; } if (pos < 0 || pos >= m_size)//pos的最大值為m_size-1 { return ; } //找到被刪除位置的下一位 for (size_t i = pos + 1; i < m_size; ++i) { m_pArr[i - 1] = m_pArr[i]; } m_size--; } int DynamicArray::findPosByValueArray(int value) { size_t nPos = -1; if (m_pArr == nullptr) { return nPos; } for (size_t i = 0; i < m_size; ++i) { if (m_pArr[i] == value) { nPos = i; break; } } return nPos; } int DynamicArray::findValueByPosArray(size_t pos) { if (m_pArr == nullptr) { return -1; } if (pos < 0 || pos >= m_size) { return -1; } return m_pArr[pos]; } void DynamicArray::reclaimSpaceArray() { if (m_size == m_capacity) { int *newArr = new int[m_capacity * 2]; if (newArr == nullptr) { cout << "new 開辟空間失敗" << endl; return; } memset(newArr, 0, m_capacity * 2 * sizeof(int));//第三個參數為字節數 memcpy(newArr, m_pArr, m_size * sizeof(int));//第三個參數為字節數 //下面這種逐個賦值的方式也可以使用 //for (size_t i = 0; i < m_capacity; i++) //{ // newArr[i] = m_pArr[i]; //} m_capacity = m_capacity * 2; if (m_pArr) { delete[] m_pArr; m_pArr = nullptr; } m_pArr = newArr; } } void DynamicArray::clearArray()//vector中clear()只是改變size的大小 { m_size = 0; } int DynamicArray::getCapacity() { return m_capacity; } int DynamicArray::getCount() { return m_size; } void DynamicArray::printArray() { for (size_t i = 0; i < m_size; ++i) { //下面兩種方式打印都可以 cout << m_pArr[i] << " "; //int ret = findValueByPosArray(i); //cout<< ret<< " "; } cout << endl; }
main.cpp
#include <iostream> #include "DynamicArray.h" using namespace std; void test() { DynamicArray * pArray = new DynamicArray; int i = 0; while (i++ < 11) { pArray->push_back_Array(i); } pArray->printArray(); cout <<"size= "<< pArray->getCount() << endl; cout << "容量: " << pArray->getCapacity() << endl; pArray->insertValueByPosArray(5,12); pArray->printArray(); cout << "insert after size= " << pArray->getCount() << endl; cout << "insert after 容量: " << pArray->getCapacity() << endl; pArray->removeByValueFromArray(2); pArray->printArray(); cout << "remove after size= " << pArray->getCount() << endl; cout << "remove after 容量: " << pArray->getCapacity() << endl; pArray->removeByPosFromArray(3); pArray->printArray(); cout << "remove by pos after size= " << pArray->getCount() << endl; cout << "remove by pos after 容量: " << pArray->getCapacity() << endl; cout<<"find 2 of pos: "<<pArray->findPosByValueArray(2)<<endl; cout << "find 8 of pos: " << pArray->findPosByValueArray(8) << endl; cout << "value at pos of 6: " << pArray->findValueByPosArray(6) << endl; pArray->clearArray(); cout << "size= " << pArray->getCount() << endl; cout << "容量: " << pArray->getCapacity() << endl; if (pArray) { delete pArray; pArray = nullptr; } } int main() { test(); return 0; }
以上代碼的運行環境為:vs2017控制臺輸出程序。
“C++怎么實現動態數組”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。