vector向量容器——STL Introduction
2012-05-02 16:31
162 查看
vector介绍:
vector容器是一个线性结构,用3个指针存放向量的起始字节位置、当前最后一个元素的末尾字节和整个容器所占的内存空间的末尾字节。如下图所示,3个指针变量分别为:
M_start、M_finish、M_end_of_storage.
![](http://ww1.sinaimg.cn/mw600/62a46728jw1dophmfl5fqj.jpg)
查看大图
图 vector容器的数据结构
vector容器在尾端插入和删除元素,算法时间复杂度为O(1)常数阶,其他元素的插入和删除为O(n)线性阶,其中n为容器的元素个数。vector容器作为数组的一个泛化推广,实现了Random Access Container、Back Insertion Sequence。
vector容器具有自动的内存管理功能,对于元素的插入和删除,可动态调整所占用的内存空间。
vector容器自带方法:
容器尾部添加元素 push_back(const T& x)
容器尾部删除元素 pop_back()
插入元素 iterator insert(iterator pos, const T& x)
删除元素 iterator erase(iterator pos)
iterator erase(iterator first, iterator last)
元素方向遍历 reverse_iterator rbegin()
reverse_iterator rend()
vector 交换 void swap(vector&)
判断vector是否为空 bool empty()
当前vector实际元素个数 size_type size()
当前vector可容纳元素个数 size_type capacity()
vector首元素(引用,要求vector非空) reference front()
vector末元素(引用,要求vector非空) reference back()
第一篇文章中提到vector容器的具有自动管理内存的功能。这里详细介绍vector容器的内存分配。
vector容器(或称vector类)将元素保存在连续的存储中。为了获得可接受的性能,vector会预先分配比所需元素更多的元素。每个将元素添加到容器的vector成员检查是否有可用空间以容纳新添加的元素。如果有,该成员在预分配的内存中下一可用位置初始化一个对象;如果没有,则重新分配vector。vector获取新的空间,将现存元素复制到新分配的空间,增加新元素,并释放旧空间。
vector所用存储开始是未构造内存,还没有存储任何对象。将元素复制或增加到这个预分配空间时,必须使用allocator类的construct成员构造元素。
以下以代码实现这个过程。
//pseduo-implementation of memory allocation strategy for vector container
template <class T> class vector {
public :
vector( ): M_start(0), M_finish(0), M_end_of_storage(0) {}
void push_back(const T&);
// ...
private:
static std::allocator<T> alloc; //object to get raw memory
void reallocate(); //get more space and copy existing element
T* M_start; //pointer to first element in the array
T* M_finish; //pointer to last element in the array
T* M_end_of_storage; //pointer to end of the space of vector
//....
};
可以使用这些指针来确定vector的大小和容量:
vector的size(实际使用)等于M_finish - M_start;
vector的capacity(在必须重新分配vector之前,可以定义的元素的综述)等于M_end_of_storage - M_start
自由空间(在需要重新分配之前,可以增加的元素的数目)是
M_end_of_storage - M_start
push_back成员使用这些指针将新元素添加到末尾
template <class T>
void vector<T>::push_back(const T& t)
{
//are we out of space?
if(M_finish == M_end_of_storage)
reallocate(); //gets more space and copies and existing elements
alloc.construct(M_finish, t);
++M_finish;
}
重新分配元素与复制元素
template <class T> void vector<T>::reallocate()
{
//compute size of current array and allocate space for twice as many elements
std::ptrdiff_t size = M_finish - M_start;
std::ptrdiff_t newcapacity = 2 * max(size,1);
//allocate space to hold newcapacity number of element of type T
T* newelements = alloc.allocate(newcapacity);
//construct copies of the existing elements in the new space
uninitialized_copy(M_start, M_finish, newelements);
//destory the old elemetn in reverse order
for(T *p = M_finish; p != M_start; /*empty*/)
alloc.destory(--p);
//deallocate cannot be called on a 0 pointer
if(M_start)
alloc.deallocate(M_start, M_end_of_storage-M_start)
//make our data structure point to the new elements
M_start = newelements;
M_finish = M_start +size;
M_end_of_storage = M_start + newcapacity;
}
vector容器是一个线性结构,用3个指针存放向量的起始字节位置、当前最后一个元素的末尾字节和整个容器所占的内存空间的末尾字节。如下图所示,3个指针变量分别为:
M_start、M_finish、M_end_of_storage.
![](http://ww1.sinaimg.cn/mw600/62a46728jw1dophmfl5fqj.jpg)
查看大图
图 vector容器的数据结构
vector容器在尾端插入和删除元素,算法时间复杂度为O(1)常数阶,其他元素的插入和删除为O(n)线性阶,其中n为容器的元素个数。vector容器作为数组的一个泛化推广,实现了Random Access Container、Back Insertion Sequence。
vector容器具有自动的内存管理功能,对于元素的插入和删除,可动态调整所占用的内存空间。
vector容器自带方法:
容器尾部添加元素 push_back(const T& x)
容器尾部删除元素 pop_back()
插入元素 iterator insert(iterator pos, const T& x)
删除元素 iterator erase(iterator pos)
iterator erase(iterator first, iterator last)
元素方向遍历 reverse_iterator rbegin()
reverse_iterator rend()
vector 交换 void swap(vector&)
判断vector是否为空 bool empty()
当前vector实际元素个数 size_type size()
当前vector可容纳元素个数 size_type capacity()
vector首元素(引用,要求vector非空) reference front()
vector末元素(引用,要求vector非空) reference back()
第一篇文章中提到vector容器的具有自动管理内存的功能。这里详细介绍vector容器的内存分配。
vector容器(或称vector类)将元素保存在连续的存储中。为了获得可接受的性能,vector会预先分配比所需元素更多的元素。每个将元素添加到容器的vector成员检查是否有可用空间以容纳新添加的元素。如果有,该成员在预分配的内存中下一可用位置初始化一个对象;如果没有,则重新分配vector。vector获取新的空间,将现存元素复制到新分配的空间,增加新元素,并释放旧空间。
vector所用存储开始是未构造内存,还没有存储任何对象。将元素复制或增加到这个预分配空间时,必须使用allocator类的construct成员构造元素。
以下以代码实现这个过程。
//pseduo-implementation of memory allocation strategy for vector container
template <class T> class vector {
public :
vector( ): M_start(0), M_finish(0), M_end_of_storage(0) {}
void push_back(const T&);
// ...
private:
static std::allocator<T> alloc; //object to get raw memory
void reallocate(); //get more space and copy existing element
T* M_start; //pointer to first element in the array
T* M_finish; //pointer to last element in the array
T* M_end_of_storage; //pointer to end of the space of vector
//....
};
可以使用这些指针来确定vector的大小和容量:
vector的size(实际使用)等于M_finish - M_start;
vector的capacity(在必须重新分配vector之前,可以定义的元素的综述)等于M_end_of_storage - M_start
自由空间(在需要重新分配之前,可以增加的元素的数目)是
M_end_of_storage - M_start
push_back成员使用这些指针将新元素添加到末尾
template <class T>
void vector<T>::push_back(const T& t)
{
//are we out of space?
if(M_finish == M_end_of_storage)
reallocate(); //gets more space and copies and existing elements
alloc.construct(M_finish, t);
++M_finish;
}
重新分配元素与复制元素
template <class T> void vector<T>::reallocate()
{
//compute size of current array and allocate space for twice as many elements
std::ptrdiff_t size = M_finish - M_start;
std::ptrdiff_t newcapacity = 2 * max(size,1);
//allocate space to hold newcapacity number of element of type T
T* newelements = alloc.allocate(newcapacity);
//construct copies of the existing elements in the new space
uninitialized_copy(M_start, M_finish, newelements);
//destory the old elemetn in reverse order
for(T *p = M_finish; p != M_start; /*empty*/)
alloc.destory(--p);
//deallocate cannot be called on a 0 pointer
if(M_start)
alloc.deallocate(M_start, M_end_of_storage-M_start)
//make our data structure point to the new elements
M_start = newelements;
M_finish = M_start +size;
M_end_of_storage = M_start + newcapacity;
}
相关文章推荐
- vector向量容器的一些基本操作
- STL(十)bit_vector位向量容器
- vector向量容器(常用的使用方法总结)
- 利用copy函数简单快速输出/保存vector向量容器中的数据
- STL 向量容器(vector)
- vector向量容器
- ACM学习历程6——Vector向量容器
- STL(六)Vector向量容器
- 如何快速方便的输出向量vector容器中不重复的内容
- C++ STL学习笔记二 vector向量容器
- C++ Vector(向量容器)
- vector向量容器
- STL系列(2):Vector 向量容器
- STL系列之deque双端队列+vector向量容器
- STL泛型编程学习之vector向量容器
- C++ STL入门教程(1)——vector(向量容器)的使用(附完整程序代码)
- C++向量容器vector的使用
- vector 向量容器
- C++ STL学习笔记二 vector向量容器
- C++ STL学习笔记六 bit_vector位向量容器