当前位置:   article > 正文

【C++】——vector容器

【C++】——vector容器

前言

对于vector,其实和string类是有点像的,但是里面的结构又有点不一样,所以有些操作是需要注意的

一 vector基本概念

1.我们使用vector的时候,可以把他当作一个数组来使用,只不过这个数组是可以自动扩容的

2.vector里面的数据是存在堆上面的,数组里面的数据是存在栈里面的,这个要区分

 3.使用vector的时候需要包含#include<vector>头文件

 

二 vector的构造函数

函数原型:

default (1)
explicit vector (const allocator_type& alloc = allocator_type());
fill (2)
explicit vector (size_type n, const value_type& val = value_type(),
                 const allocator_type& alloc = allocator_type());
range (3)
template <class InputIterator>
         vector (InputIterator first, InputIterator last,
                 const allocator_type& alloc = allocator_type());
copy (4)
vector (const vector& x);

1.vector (const allocator_type& alloc = allocator_type());//无参构造函数

2.vector (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type());//将n个val拷贝给本身

3.vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());//使用迭代器把区间内的元素给本身

4.vector (const vector& x);//拷贝构造函数

1.explicit是限制隐式类型转换的。

2.对于第三个迭代器来说,设置为模板,这样就可以使用其他类型,而不单单限制于一种类型

3.对于const allocator_type& alloc = allocator_type()这个来说,他只是一个内存池,如果我们不写就用这个默认的,除非你觉得你比他的写得好,你就可以传进去声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】

推荐阅读