C++ <vector>
2015-09-07 14:34
337 查看
C++ 头文件<vector>
1.头文件结构
template < class T, class Alloc = allocator<T> > class vector; // generic template
- classes
-
vector(类模板)
-
vector<bool>(类模板)
- Functions
-
begin(函数模板,起始迭代器)
-
end(函数模板,超尾迭代器)
2.vector类模板
vector是用于存放一组相同类型的元素的容器,性质类似于数组,但是它是可以自动分配大小的动态数组。因为需要动态分配空间,所以当插入元素时需要重新分配,如果每一次插入都重新分配,消耗会很大,所以会提前确定比实际需要占用空间略大的capacity(容量)
1.容器属性
严格的线性序列可以直接访问序列的元素,甚至可以用指针访问
可以在末尾快速插入删除元素
2.模板参数
T:(当需要重新分配空间时,需要拷贝到新空间并且插入元素)Alloc:用来定义分配内存空间的模型,默认是allocator
3.成员类型
成员类型 | 定义 | 注释说明 |
---|---|---|
value_type | 第一个模板参数 | \ |
allocator_type | 模板第二个默认参数 | allocator |
reference | allocator_type::reference | 默认的分配器:value_type & |
const_reference | allocator_type::const_reference | 默认分配器:const value_type & |
pointer | allocator_type::pointer | 默认分配器:value_type* |
const_pointer | allocator_type::const_pointer | const value_type* |
iterator | 迭代器 | 可以转换成const_iterator |
const_iterator | 常量迭代器 | \ |
reverse_iterator | 反向迭代器 | \ |
const_reverse_iterator | 常量反向迭代器 | \ |
different_type | 有符号整型:iterator_traits::difference_type | \ |
size_type | 无符号整型,可以表达任何非负值的difference_type | \ |
3.成员函数
1.默认函数
构造函数空构造函数(默认构造函数)
fill constructor 用一个种类的元素的复制填充容器
range constructor 用区间[first,last)的数初始化vector
拷贝构造函数
析构函数
赋值操作符函数
2.迭代器
beginend
rbegin
rend
cbegin
cend
crbegin
crend
3.容量
sizemax_size
resize
capacity
empty
reverse
shrink_to_fit
4.元素访问
operator[]at
front
back
data
5.编辑修饰
assignpush_back
pop_back
insert
erase
swap
clear
emplace
emplace_back
6.分配者
get_allocator7.非成员函数重载
relation operatorsswap
8.模板特化
vector<bool>
相关文章推荐
- C++ Dynamic_cast
- gcc 后 -l 和 -L的区别
- C++List用法
- Item 23:非成员非友元函数好于成员函数 Effective C++笔记
- c++:sprintf应用实例
- C++泛型算法
- C++ 16进制转10进制
- C++ 分离编译 多个文件
- C++数组做参数
- c/c++的memset()函数
- 黑马程序员-C语言基础之结构体数组
- C语言指针使用总结
- effective c++ 确定对象被使用前已经被初始化
- Effective C++——条款29(第5章)
- c语言之数据拷贝memcpy strcpy
- 随手写了一段C++访问LDAP, 并且获取sid的代码
- C++ 栈和队列
- C++学习笔记(六)文件处理
- 详解C++编程中的变量相关知识
- C++项目中的extern "C" {}