C++Vector容器常用函數(shù)接口詳解
一、基礎(chǔ)框架
template<class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
private:
iterator _start;//指向第一個元素
iterator _finish;//指向最后一個元素的下一個位置
iterator _endofstoage;//容量
};二、迭代器實(shí)現(xiàn)
const_iterator begin() const
{
return _str;
}
const_iterator end() const
{
return _str + _size;
}
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}三、size capacity resize reserve
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _endofstoage - _start;
}
void reserve(size_t n)
{
size_t sz = size();
if (n > capacity())
{
T* tmp = new T[n];
//T* tmp = (T*)malloc(sizeof(T)*n);
if (_start)
{
//memcpy(tmp, _start, size()*sizeof(T));
for (size_t i = 0; i < size(); ++i)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
}
_finish = _start + sz;
_endofstoage = _start + n;
}
//void resize(size_t n, const T& val = T())
void resize(size_t n, T val = T())//T類型的匿名對象做缺省參數(shù),調(diào)用T的默認(rèn)構(gòu)造函數(shù)
{
if (n > capacity())
{
reserve(n);
}
if (n > size())
{
while (_finish < _start + n)
{
*_finish = val;
++_finish;
}
}
else
{
_finish = _start + n;
}
}注意點(diǎn):在reservr函數(shù)中,在拷貝的時候,不可以簡單的通過memcpy函數(shù)來淺拷貝,因?yàn)楫?dāng)T是涉及到深淺拷貝的類型時,使用memcpy會存在深淺拷貝釋放內(nèi)存空間的問題。
四、insert,erase
iterator insert(iterator pos, const T& x)
{
// 檢查參數(shù)
assert(pos >= _start && pos <= _finish);
// 擴(kuò)容
// 擴(kuò)容以后pos就失效了,需要更新一下
if (_finish == _endofstoage)
{
size_t n = pos - _start;
size_t newCapacity = capacity() == 0 ? 4 : capacity() * 2;
reserve(newCapacity);
pos = _start + n;
}
// 挪動數(shù)據(jù)
iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
--end;
}
*pos = x;
++_finish;
return pos;
}
iterator erase(iterator pos)
{
assert(pos >= _start && pos < _finish);
iterator it = pos + 1;
while (it != _finish)
{
*(it - 1) = *it;
++it;
}
--_finish;
return pos;
}注意點(diǎn):在insert函數(shù)中,如果需要擴(kuò)容的話,注意擴(kuò)容前后pos位置的更新,其實(shí)STL庫中也進(jìn)行了這樣的更新,不更新的話位置就失效了 。
五、pop_back,push_back
void push_back(const T& x)
{
/*if (_finish == _endofstoage)
{
size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
reserve(newcapacity);
}
*_finish = x;
_finish++;*/
insert(end(), x);
}
void pop_back()
{
erase(end() - 1);//復(fù)用
}注意點(diǎn):可以直接復(fù)用insert和erase函數(shù)。
六、operator[]
T& operator[](size_t pos)
{
assert(pos < size());
return *(_start + pos);
}
const T& operator[](size_t pos) const
{
assert(pos < size());
return *(_start + pos);
}注意點(diǎn):分別針對常對象和普通對象。
七、構(gòu)造函數(shù) 析構(gòu)函數(shù) 賦值重載
vector()
:_start(nullptr)
, _finish(nullptr)
, _endofstoage(nullptr)
{}
//為什么要有這個
//是為了拷貝構(gòu)造的現(xiàn)代寫法時有一個可用的有參構(gòu)造可以用
template <class InputIterator>
vector(InputIterator first, InputIterator last)
: _start(nullptr)
, _finish(nullptr)
, _endofstoage(nullptr)
{
while (first != last)
{
push_back(*first);
++first;
}
}
//n個val調(diào)用的構(gòu)造函數(shù)
vector(size_t n, const T& val = T())//用一個匿名對象做缺省參數(shù)
: _start(nullptr)
, _finish(nullptr)
, _endofstoage(nullptr)
{
reserve(n);
for (size_t i = 0; i < n; ++i)
{
push_back(val);
}
}
vector(int n, const T& val = T())
: _start(nullptr)
, _finish(nullptr)
, _endofstoage(nullptr)
{
reserve(n);
for (int i = 0; i < n; ++i)
{
push_back(val);
}
}
void swap(vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endofstoage, v._endofstoage);
}
//vector(const vector& v);
vector(const vector<T>& v)
: _start(nullptr)
, _finish(nullptr)
, _endofstoage(nullptr)
{
vector<T> tmp(v.begin(), v.end());
swap(tmp);
}
//vector& operator=(vector v)
vector<T>& operator=(vector<T> v)
{
swap(v);
return *this;
}
// 資源管理
~vector()
{
if (_start)
{
delete[] _start;
_start = _finish = _endofstoage = nullptr;
}
}注意點(diǎn)1: 賦值重載的形參列表利用傳值傳參,調(diào)用了拷貝構(gòu)造完成了深拷貝,直接交換!
注意點(diǎn)2:注意這種拷貝構(gòu)造和賦值重載的現(xiàn)代寫法(請人干活,竊取果實(shí)),但必須得有對應(yīng)的有參構(gòu)造!
到此這篇關(guān)于C++Vector容器常用函數(shù)接口詳解的文章就介紹到這了,更多相關(guān)C++ Vector容器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
本篇文章是對求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++無鎖數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了C++無鎖數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12

