C++ STL之vector

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_typesize_tvalue_typeint。对应实际中使用到的构造函数如下

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支持操作

  1. void push_back(value_type &&__x)在数组的最后添加元素,参数为右值引用(底层调用的应该是移动构造函数?)
  2. void push_back(const value_type &__x)在数组最后添加元素,可接受常量和普通变量(常用形式)
  3. void pop_back()把数组最后一个元素删除
  4. vector<int>::iterator begin()返回指向数组首元素的迭代器
  5. vector<int>::iterator end()返回指向尾元素后一个位置的迭代器(用作边界判断)
  6. vector<int>::const_iterator cbegin()返回指向首元素的常量迭代器
  7. vector<int>::const_iterator cend()返回指向尾元素后一位置的常量迭代器
  8. reference front()返回数组首元素的引用(左值,可赋值)
  9. reference back()返回数组尾元素的引用(左值,可赋值)
  10. size_type size()返回数组实际存储元素个数
  11. size_type capacity()返回数组所有可使用的存储空间
  12. unsigned long long int max_size()返回数组最大可用空间
  13. bool empty()返回数组是否为空,为空返回true
  14. void clear()将数组清空
  15. reference at(size_type __n)返回索引为n的元素的引用,越界抛出异常
  16. iterator insert(const_iterator __position, value_type &&__x)插入元素,成功返回插入位置的迭代器,失败返回尾迭代器
  17. iterator insert(const_iterator __position, const value_type &_x)从__position所指地方(之前)插入元素其他几种形式类似
  18. iterator insert(const_iterator __position, initializer_list<value_type> __l)
  19. iterator insert(const_iterator __position, size_type __n, const value_type &__x)
  20. iterator insert(const_iterator __position, _InputIterator __first, _InputIterator __last)将__first到不包括__last插入
  21. void assign(size_type __n, const value_type &__val)三种形式,原数组都要清空重来,赋值为n个val的元素
  22. void assign(initializer_list<value_type> __l)
  23. void assign(_InputIterator __first, _InputIterator __last)用迭代器范围内,不包括__last元素赋值
  24. void resize(size_type __new_size)重塑数组大小,新构造的元素值初始化,__new_size小于当前size,截断
  25. void resize(size_type __new_size, const value_type &__x)新构造元素值为__x
  26. void emplace_back(_Args __args...)通过__args构造元素类型添加到尾部
  27. iterator void emplace(const_iterator __position, _Args __args...)在指定位置__position构造元素添加,返回插入位置迭代器
  28. iterator erase(const_iterator __position)删除迭代器指向的元素,返回删除后第一个元素的迭代器
  29. iterator erase(const_iterator __first, const_iterator __last)区间删除,不包括__last
  30. void reserve(size_type __n)提前预留一定的空间,避免重复动态扩充
  31. void shrink_to_fit()将capacity将至实际使用的size,释放多余空间
  32. void swap(vector<int, allocator<int>> &__x)用__x替换当前数组
  33. value_type* data()返回一个直接指向内存中存储vector元素位置的指针
  34. 还有其他的例如rbegin()、rend()之类的不详细介绍了

关于空间的解释

vector类动态扩充空间是指当capacity没有充足空间时,扩充至1.5-2倍(多数为2倍,由编译器决定),将原来的元素移动过去,再继续添加新元素。所以如果最开始知道需要多少元素,可以使用reserve()函数分配指定空间的容量,或者在确定不会新增元素之后调用shrink_to_fit().

小结

上述内容可确保大部分正确,有些我有疑惑的地方也以问句形式标记,此外还有一些是我暂时不理解或者错误的地方,希望大家看到了可以指正。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值