您的位置:首页 > 编程语言 > C语言/C++

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.迭代器

begin

end

rbegin

rend

cbegin

cend

crbegin

crend

3.容量

size

max_size

resize

capacity

empty

reverse

shrink_to_fit

4.元素访问

operator[]

at

front

back

data

5.编辑修饰

assign

push_back

pop_back

insert

erase

swap

clear

emplace

emplace_back

6.分配者

get_allocator

7.非成员函数重载

relation operators

swap

8.模板特化

vector
<bool>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: