vector简介
vector是C++ STL中最常使用的模板类,定义在vector头文件中,std命名空间内,支持内置类型和自定义类型。vector模板类很好地支持动态扩容,其底层实现虽然是malloc动态分配内存,但是经过封装,避免我们在使用内置数组的内存泄漏问题。定义数组容器的格式如下vector<int> a;
其中int
类型可以为任意内置类型或者其他可以访问到的自定义类型。
vector支持的操作
vector定义的类型
typedef _Tp value_type; //数组存的值类型
typedef typename _Base::pointer pointer;
typedef typename _Alloc_traits::const_pointer const_pointer;
typedef typename _Alloc_traits::reference reference;
typedef typename _Alloc_traits::const_reference const_reference;
typedef __gnu_cxx::__normal_iterator<pointer, vector> iterator; //迭代器类型
typedef __gnu_cxx::__normal_iterator<const_pointer, vector> const_iterator; //const迭代器
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator; //反向迭代器
typedef size_t size_type; //下标索引类型以及返回的数组大小的类型
typedef ptrdiff_t difference_type; //迭代器相减的类型
typedef _Alloc allocator_type; //用于数组底层实现的分配器的类型(参见《STL源码剖析》)
vector构造函数
vector<int>() //默认构造函数
vector<int>(vector<int> &&__x) //移动构造函数
vector<int>(const vector<int> &__x) //拷贝构造函数
vector<int>(size_type __n)
vector<int>(size_type __n, const value_type &__value)
vector<int>(initializer_list<value_type> __l)
vector<int>(_InputIterator __first, _InputIterator __last)
上述是vector支持的构造函数,其中还有其他3个涉及到allocator_type
的我暂时还没有真正用到过。对于上述整型数组,size_type
为size_t
,value_type
为int
。对应实际中使用到的构造函数如下
vector<int> a; //调用默认构造函数
vector<int> b(); //同样调用默认构造函数??
vector<int> c(a); //调用拷贝构造函数vector<int>(const vector<int> &__x)
vector<int> d(std::move(a)); //调用移动构造函数,
vector<int> e(10); //构造size大小为10的数组,数组元素是自定义类型则默认初始化,内置类型值初始化
vector<int> f(10, 1); //构造size大小为10的数组,初始化为1
vector<int> g(f.begin(), f.begin()+5); //从迭代器__first所指到__last(不包括)截止的内容初始化数组
vector支持操作
void push_back(value_type &&__x)
在数组的最后添加元素,参数为右值引用(底层调用的应该是移动构造函数?)void push_back(const value_type &__x)
在数组最后添加元素,可接受常量和普通变量(常用形式)void pop_back()
把数组最后一个元素删除vector<int>::iterator begin()
返回指向数组首元素的迭代器vector<int>::iterator end()
返回指向尾元素后一个位置的迭代器(用作边界判断)vector<int>::const_iterator cbegin()
返回指向首元素的常量迭代器vector<int>::const_iterator cend()
返回指向尾元素后一位置的常量迭代器reference front()
返回数组首元素的引用(左值,可赋值)reference back()
返回数组尾元素的引用(左值,可赋值)size_type size()
返回数组实际存储元素个数size_type capacity()
返回数组所有可使用的存储空间unsigned long long int max_size()
返回数组最大可用空间bool empty()
返回数组是否为空,为空返回truevoid clear()
将数组清空reference at(size_type __n)
返回索引为n的元素的引用,越界抛出异常iterator insert(const_iterator __position, value_type &&__x)
插入元素,成功返回插入位置的迭代器,失败返回尾迭代器iterator insert(const_iterator __position, const value_type &_x)
从__position所指地方(之前)插入元素其他几种形式类似iterator insert(const_iterator __position, initializer_list<value_type> __l)
iterator insert(const_iterator __position, size_type __n, const value_type &__x)
iterator insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
将__first到不包括__last插入void assign(size_type __n, const value_type &__val)
三种形式,原数组都要清空重来,赋值为n个val的元素void assign(initializer_list<value_type> __l)
void assign(_InputIterator __first, _InputIterator __last)
用迭代器范围内,不包括__last元素赋值void resize(size_type __new_size)
重塑数组大小,新构造的元素值初始化,__new_size小于当前size,截断void resize(size_type __new_size, const value_type &__x)
新构造元素值为__xvoid emplace_back(_Args __args...)
通过__args构造元素类型添加到尾部iterator void emplace(const_iterator __position, _Args __args...)
在指定位置__position构造元素添加,返回插入位置迭代器iterator erase(const_iterator __position)
删除迭代器指向的元素,返回删除后第一个元素的迭代器iterator erase(const_iterator __first, const_iterator __last)
区间删除,不包括__lastvoid reserve(size_type __n)
提前预留一定的空间,避免重复动态扩充void shrink_to_fit()
将capacity将至实际使用的size,释放多余空间void swap(vector<int, allocator<int>> &__x)
用__x替换当前数组value_type* data()
返回一个直接指向内存中存储vector元素位置的指针- 还有其他的例如
rbegin()、rend()
之类的不详细介绍了
关于空间的解释
vector类动态扩充空间是指当capacity没有充足空间时,扩充至1.5-2倍(多数为2倍,由编译器决定),将原来的元素移动过去,再继续添加新元素。所以如果最开始知道需要多少元素,可以使用reserve()
函数分配指定空间的容量,或者在确定不会新增元素之后调用shrink_to_fit()
.
小结
上述内容可确保大部分正确,有些我有疑惑的地方也以问句形式标记,此外还有一些是我暂时不理解或者错误的地方,希望大家看到了可以指正。