c++ vector模擬實(shí)現(xiàn)代碼
vector的介紹
1、vector是表示可變大小數(shù)組的序列容器。
2、就像數(shù)組一樣,vector也采用的連續(xù)存儲(chǔ)空間來存儲(chǔ)元素。也就是意味著可以采用下標(biāo)對(duì)vector的元素進(jìn)行訪問,和數(shù)組一樣高效。但是又不像數(shù)組,它的大小是可以動(dòng)態(tài)改變的,而且它的大小會(huì)被容器自動(dòng)處理。
3、本質(zhì)講,vector使用動(dòng)態(tài)分配數(shù)組來存儲(chǔ)它的元素。當(dāng)新元素插入時(shí)候,這個(gè)數(shù)組需要被重新分配大小為了增加存儲(chǔ)空間。其做法是,分配一個(gè)新的數(shù)組,然后將全部元素移到這個(gè)數(shù)組。就時(shí)間而言,這是一個(gè)相對(duì)代價(jià)高的任務(wù),因?yàn)槊慨?dāng)一個(gè)新的元素加入到容器的時(shí)候,vector并不會(huì)每次都重新分配大小。
4、vector分配空間策略:vector會(huì)分配一些額外的空間以適應(yīng)可能的增長(zhǎng),因?yàn)榇鎯?chǔ)空間比實(shí)際需要的存儲(chǔ)空間更大。不同的庫采用不同的策略權(quán)衡空間的使用和重新分配。但是無論如何,重新分配都應(yīng)該是對(duì)數(shù)增長(zhǎng)的間隔大小,以至于在末尾插入一個(gè)元素的時(shí)候是在常數(shù)時(shí)間的復(fù)雜度完成的。
5、因此,vector占用了更多的存儲(chǔ)空間,為了獲得管理存儲(chǔ)空間的能力,并且以一種有效的方式動(dòng)態(tài)增長(zhǎng)。
6、與其它動(dòng)態(tài)序列容器相比(deques, lists and forward_lists), vector在訪問元素的時(shí)候更加高效,在末尾添加和刪除元素相對(duì)高效。對(duì)于其它不在末尾的刪除和插入操作,效率更低。比起lists和forward_lists統(tǒng)一的迭代器和引用更好。
vector是C++ STL中一個(gè)非常重要的容器,了解 vector 的底層實(shí)現(xiàn)原理,可以很好的幫助我們更加熟練的使用vector。
c++ vector 模擬實(shí)現(xiàn)代碼:
#include<iostream>
using namespace std;
namespace bit
{
template<typename T>
class vector
{
public:
typedef T* iterator;
public:
T operator[](int i)
{
return start[i];
}
public:
vector() :start(nullptr), finish(nullptr), end_of_sorage(nullptr)
{
}
vector(size_t n, const T& value = T()) :start(nullptr), finish(nullptr), end_of_sorage(nullptr)
{
reserve(n);//先擴(kuò)容
while (n--!=0) //再填充
{
push_back(value);
}
}
template<class InPutIterator> //由前后指針來創(chuàng)建
vector(InPutIterator first, InPutIterator last):start(nullptr), finish(nullptr), end_of_sorage(nullptr)
{
reserve(last-first);//先申請(qǐng)空間
while (first != last)
{
push_back(*first);
first++;
}
}
~vector()
{
delete[]start;
start = finish = end_of_sorage = nullptr;
}
public:
int size()
{
return finish - start;
}
int capacity()
{
return end_of_sorage - start;
}
bool empty()
{
return finish == start;
}
void swap(vector<T>& v)
{
std::swap(start, v.start);
std::swap(finish, v.finish);
std::swap(end_of_sorage, v.end_of_sorage);
}
void reserve(size_t new_capacity) // 擴(kuò)容
{
if (new_capacity > capacity())
{
int old_size = size(); //原來的大小
T* newV = new T[new_capacity]; //新申請(qǐng)空間
if (start)//當(dāng)原有內(nèi)容不空時(shí)
{
for (int i = 0; i < size(); i++) //復(fù)制進(jìn)新空間
{
newV[i] = start[i];
}
}
delete[]start;//刪除原有空間
start = newV;//指向新空間
finish = start + old_size;
end_of_sorage = start + new_capacity;
}
}
void resize(int new_size, const T& value = 0) //擴(kuò)充大小
{
if (new_size <= size())
{
finish = start + new_size;
}
if (new_size > capacity())
{
reserve(new_size * 2);
}
iterator p = finish;
finish = start + new_size;//指向新大小
while (p != finish) //填充value
{
*p = value;
p++;
}
}
public:
void push_back(const T &c)
{
insert(end(), c);
}
public:
typedef T* iterator;
iterator begin()
{
return start;
}
iterator end()
{
return finish;
}
public:
iterator insert(iterator pos, const T &x) //在pos位置前插入x
{
if (size() + 1 >= capacity())
{
size_t oldpos = pos - start;
size_t new_capacity = capacity() ? (capacity() * 2) : 1;
reserve(new_capacity);
pos = start + oldpos;
}
T* p = finish;
for (; p != pos; p--)
{
*p = *(p - 1);
}
*p = x;
finish++;
return pos;
}
iterator erase(iterator pos) //刪除pos位置值
{
T* p = pos;
while (p != finish - 1)
{
*p = *(p + 1);
p++;
}
finish--;
return pos;
}
private:
T* start;//指向最開始
T* finish;//指向最后一個(gè)元素的下一個(gè)位置
T* end_of_sorage;//指向最大容量的下一個(gè)位置
};
}
int main()
{
int ar[] = { 1,2,3,4,5,6,7,7 };
bit::vector<int>v1(ar, ar + 6);
bit::vector<int>v2;
bit::vector<int>v3(10,'a');
v1.erase(v1.end()-1);
v1.insert(v1.begin(), 0);
v1.swap(v3);
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
return 0;
}
總結(jié)
以上所述是小編給大家介紹的c++ vector模擬實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
C語言實(shí)現(xiàn)BMP格式圖片轉(zhuǎn)化為灰度
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)BMP格式圖片轉(zhuǎn)化為灰度,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
C++中set/multiset與map/multimap的使用詳解
這篇文章主要為大家詳細(xì)介紹了C++中set/multiset與map/multimap的使用,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-02-02
C++與QML進(jìn)行數(shù)據(jù)交互實(shí)現(xiàn)方式介紹
迫于無奈開始寫android的程序,以前使用QWidget的方式試過,雖然界面可以實(shí)現(xiàn),但是最后調(diào)用攝像頭時(shí),未能成功,再?zèng)]有繼續(xù)。這幾天開始使用qml進(jìn)行嘗試,在使用的過程中,其中的一個(gè)難點(diǎn),就是在qml與c++中數(shù)據(jù)的交互2022-09-09
C/C++?Qt?數(shù)據(jù)庫與TreeView組件綁定詳解
本篇文章主要介紹了QT數(shù)據(jù)庫與View組件的綁定,通過數(shù)據(jù)庫與組件關(guān)聯(lián)可實(shí)現(xiàn)動(dòng)態(tài)展示數(shù)據(jù)庫中的表記錄。感興趣的小伙伴可以了解一下2021-12-12
C語言中字符和字符串處理(ANSI字符和Unicode字符)
這篇文章主要介紹了C語言與C++中字符和字符串處理(ANSI字符和Unicode字符)的詳細(xì)內(nèi)容,非常的全面,這里推薦給大家,希望大家能夠喜歡。2015-03-03
關(guān)于memcpy和memmove的一點(diǎn)重要說明
下面小編就為大家?guī)硪黄P(guān)于memcpy和memmove的一點(diǎn)重要說明。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12

